nestjs 代码中可空值的可空数组第一个 graphql

问题描述

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

example 表明

  @Field(type => [String])
  ingredients: string[];

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

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

解决方法

您需要将 @Field(() => [String],{ nullable: 'itemsAndList' }) 设置为 described in the docs

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

@Field(type => [Post])
posts: Post[];

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

要声明数组的项(不是数组本身)可以为 null,请将 nullable 属性设置为 'items',如下所示:

@Field(type => [Post],{ nullable: 'items' })
posts: Post[];`

如果数组及其项都可以为空,请将 nullable 设置为 'itemsAndList'。