GraphQLError: Type <TYPE> must define one or more fields

780 views Asked by At

Problem

When I try to start this Federated Property Service, I get an error that says: GraphQLError: Type Property must define one or more fields.

The Schema that it generates looks correct, and has those fields populated, but when I add the @Directive('@key(fields: "id")') It generates this error.

Property Model

import { Directive, Field, ID, ObjectType } from "@nestjs/graphql";

@ObjectType()
export class Address {
  @Field()
  addressLineOne: string;

  @Field()
  addressLineTwo: string;

  @Field()
  addressLineThree: string;

  @Field()
  postcode: string;
}

@ObjectType()
@Directive('@key(fields: "id")')
export class Property {
  @Field((type) => ID)
  id: string;

  @Field()
  owner: string;

  @Field()
  propertyUPRN: string;

  @Field()
  address: Address;
}

Property Resolver

import { Query, Resolver, ResolveReference } from "@nestjs/graphql";
import { Property } from "./models/property.model";
import { PropertiesService } from "./properties.service";

@Resolver((of) => Property)
export class PropertiesResolver {
  constructor(private readonly propertiesService: PropertiesService) {}

  @Query((returns) => [Property])
  public async findAllProperties(): Promise<Property[]> {
    return this.propertiesService.findAll();
  }

  @ResolveReference()
  public async resolveReference(ref: { __typename: string; id: string }): Promise<Property> {
    return await this.propertiesService.findOneById(ref.id);
  }
}

Auto Generated Schema

# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

directive @key(fields: String!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE

directive @extends on OBJECT | INTERFACE

directive @external on OBJECT | FIELD_DEFINITION

directive @requires(fields: String!) on FIELD_DEFINITION

directive @provides(fields: String!) on FIELD_DEFINITION

directive @shareable on FIELD_DEFINITION | OBJECT

directive @link(url: String!, import: [link__Import]) on SCHEMA

directive @tag(name: String!) repeatable on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION

directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION

directive @override(from: String!) on FIELD_DEFINITION

type Address {
  addressLineOne: String!
  addressLineTwo: String!
  addressLineThree: String!
  postcode: String!
}

type Property {
  id: ID!
  owner: String!
  propertyUPRN: String!
  address: Address!
}

type Query {
  findAllProperties: [Property!]!
}

scalar link__Import

Package JSON

{
  "name": "nest-typescript-starter",
  "version": "1.0.0",
  "description": "Nest TypeScript starter repository",
  "license": "MIT",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint '{src,apps,libs,test}/**/*.ts' --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "echo 'No e2e tests implemented yet.'"
  },
  "dependencies": {
    "@apollo/federation": "^0.38.1",
    "@apollo/subgraph": "^2.2.2",
    "@nestjs/apollo": "10.0.17",
    "@nestjs/common": "9.0.1",
    "@nestjs/core": "9.0.1",
    "@nestjs/graphql": "10.0.18",
    "@nestjs/mongoose": "^9.2.1",
    "@nestjs/platform-express": "9.0.1",
    "apollo-server": "3.9.0",
    "apollo-server-express": "3.9.0",
    "class-transformer": "0.5.1",
    "class-validator": "0.13.2",
    "graphql": "16.5.0",
    "graphql-query-complexity": "0.11.0",
    "graphql-subscriptions": "2.0.0",
    "mongoose": "^6.8.0",
    "reflect-metadata": "0.1.13",
    "rxjs": "7.5.5"
  },
  "devDependencies": {
    "@nestjs/cli": "9.0.0",
    "@nestjs/schematics": "9.0.1",
    "@nestjs/testing": "9.0.1",
    "@types/express": "4.17.13",
    "@types/node": "18.0.3",
    "@types/supertest": "2.0.12",
    "@typescript-eslint/eslint-plugin": "5.30.5",
    "@typescript-eslint/parser": "5.30.5",
    "eslint": "8.19.0",
    "eslint-config-prettier": "8.5.0",
    "eslint-plugin-import": "2.26.0",
    "jest": "28.1.2",
    "prettier": "2.7.1",
    "supertest": "6.2.4",
    "ts-jest": "28.0.5",
    "ts-loader": "9.3.1",
    "ts-node": "10.8.2",
    "tsconfig-paths": "4.0.0",
    "typescript": "4.7.4"
  }
}
0

There are 0 answers