【问题标题】:nullable array of nullable values in nestjs code first graphql嵌套js代码中可空值的可空数组首先graphql
【发布时间】:2021-09-02 18:57:03
【问题描述】:

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

example 表明

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

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

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

【问题讨论】:

    标签: graphql nestjs code-first


    【解决方案1】:

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

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

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

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

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

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

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

    【讨论】:

    • 呵呵,真丢人。文档中很清楚。我想我有点太懒了。我希望问题的措辞对将来的其他懒人有所帮助:)
    • 作为一个懒人发现这很有帮助,谢谢! ?
    猜你喜欢
    • 2020-01-09
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多