输入对象类型`TypeName`必须定义一个或多个字段

Posted

技术标签:

【中文标题】输入对象类型`TypeName`必须定义一个或多个字段【英文标题】:Input Object type `TypeName` must define one or more fields 【发布时间】: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 中的所有字段都按预期工作。我做错了什么?

【问题讨论】:

【参考方案1】:

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

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

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

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

【讨论】:

以上是关于输入对象类型`TypeName`必须定义一个或多个字段的主要内容,如果未能解决你的问题,请参考以下文章

具有多个单元格模板的 CollectionView(使用未声明的类型,<typeName> 在此上下文中的类型查找不明确)

“类型 RootQueryType 必须定义一个或多个字段。”

GraphQLError:类型查询必须定义一个或多个字段。使用类型鹅

c#或java如何用循环定义多个变量

4复合类型

C++Primer 第十六章