2
votes

I am experimenting with NestJS and TypeGraphQL. And I have an example model of a cat.

import { Document } from 'mongoose';
import { ObjectType, InputType, Field, ID } from 'type-graphql';

export interface Cat extends Document {
  readonly name: string;
  readonly age?: number;
}

class CatBase {
  @Field()
  name: string;

  @Field({ nullable: true })
  age?: number;
}

@ObjectType()
export class CatObjectType extends CatBase implements Cat {
  @Field(type => ID)
  id: string;
}

@InputType()
export class CatInputType extends CatBase implements Cat {
}

Here I am trying to reuse BaseCat in CatObjectType and CatInputType. But I getting this error:

[ { GraphQLError: Input Object type CatInputType must define one or more fields.
      at SchemaValidationContext.reportError (/Users/pavel/Dev/exampleproject/node_modules/graphql/type/validate.js:90:19)
      at validateInputFields (/Users/pavel/Dev/exampleproject/node_modules/graphql/type/validate.js:432:13)
      at validateTypes (/Users/pavel/Dev/exampleproject/node_modules/graphql/type/validate.js:240:7)
      at validateSchema (/Users/pavel/Dev/exampleproject/node_modules/graphql/type/validate.js:54:3)
      at graphqlImpl (/Users/pavel/Dev/exampleproject/node_modules/graphql/graphql.js:79:62)
      at /Users/pavel/Dev/exampleproject/node_modules/graphql/graphql.js:28:59
      at new Promise (<anonymous>)
      at Object.graphql (/Users/pavel/Dev/exampleproject/node_modules/graphql/graphql.js:26:10)
      at Function.<anonymous> (/Users/pavel/Dev/exampleproject/node_modules/type-graphql/dist/schema/schema-generator.js:18:52)
      at Generator.next (<anonymous>)
      at /Users/pavel/Dev/exampleproject/node_modules/tslib/tslib.js:110:75
      at new Promise (<anonymous>)
      at Object.__awaiter (/Users/pavel/Dev/exampleproject/node_modules/tslib/tslib.js:106:16)
      at Function.generateFromMetadata (/Users/pavel/Dev/exampleproject/node_modules/type-graphql/dist/schema/schema-generator.js:15:24)
      at /Users/pavel/Dev/exampleproject/node_modules/type-graphql/dist/utils/buildSchema.js:11:65
      at Generator.next (<anonymous>)
    message:
     'Input Object type CatInputType must define one or more fields.' } ]
(node:72485) UnhandledPromiseRejectionWarning: Error: Generating schema error
    at Function.<anonymous> (/Users/pavel/Dev/exampleproject/node_modules/type-graphql/dist/schema/schema-generator.js:20:27)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/pavel/Dev/exampleproject/node_modules/tslib/tslib.js:107:62)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:72485) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:72485) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

When in CatInputType are described all fields from BaseCat all work as expected. What I am doing wrong?

1

1 Answers

7
votes

TypeGraphQL by default doesn't inherit all @Fields from parent classes that are not decorated with @InputType or @ObjectType because this types should not be mixed as things like unions and interfaces are invalid GraphQL input types.

So if you are sure you're not violating this rules, just place both decorators on top of the class:

@ObjectType({ isAbstract: true })
@InputType({ isAbstract: true })
class CatBase {
  @Field()
  name: string;

  @Field({ nullable: true })
  age?: number;
}