Nestjs 代码中可空值的可空数组首先 graphql

die*_*edu 5 code-first graphql nestjs

在 Nestjs graphql 中使用代码优先方法时,如何获得允许可为空值的数组类型字段。

示例表明

  @Field(type => [String])
  ingredients: string[];
Run Code Online (Sandbox Code Playgroud)

[String!]!文件中生成schema.gql。我怎样才能得到公正[String]?使用{nullable: true}给了我[String!]

我希望在装饰器中找到某种类型的实用程序或参数@Field,但似乎并非如此

Jay*_*iel 12

您需要@Field(() => [String], { nullable: 'itemsAndList' })按照文档中的描述进行设置

当字段是数组时,我们必须在Field()装饰器的type函数中手动指明数组类型,如下所示:

@Field(type => [Post])
posts: Post[];
Run Code Online (Sandbox Code Playgroud)

提示 使用数组括号符号([ ]),我们可以指示数组的深度。例如,使用 [[Int]] 将表示一个整数矩阵。

要声明数组的项目(不是数组本身)可为空,请将可为空属性设置为“items”,如下所示:

@Field(type => [Post])
posts: Post[];
Run Code Online (Sandbox Code Playgroud)

如果数组及其项均可为 null,请将 nullable 设置为 'itemsAndList'。

  • 呵呵,羞死我了。文档中很清楚。我想我有点太懒了。我希望这个问题的措辞将来能帮助其他懒人:) (4认同)