【问题标题】:Input Object type `TypeName` must define one or more fields输入对象类型`TypeName`必须定义一个或多个字段
【发布时间】:2020-04-19 00:33:48
【问题描述】:

我正在试验NestJSTypeGraphQL。我有一个猫的示例模型。

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 {
}

这里我试图在CatObjectTypeCatInputType 中重用BaseCat。但我收到此错误:

[ { 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.

CatInputType 中描述时,BaseCat 中的所有字段都按预期工作。我做错了什么?

【问题讨论】:

    标签: graphql nestjs typegraphql


    【解决方案1】:

    默认情况下,TypeGraphQL 不会从未使用 @InputType@ObjectType 修饰的父类继承所有 @Fields,因为此类类型不应混合,因为联合和接口之类的东西是无效的 GraphQL 输入类型。

    因此,如果您确定自己没有违反此规则,只需将两个装饰器放在类的顶部即可:

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

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 2020-03-09
      • 2020-02-25
      • 2020-08-10
      • 2017-06-12
      • 2016-06-29
      • 2017-12-12
      • 2021-10-08
      • 2019-12-31
      相关资源
      最近更新 更多