【问题标题】:graphql node api: how can I define a field as optional?graphql node api:如何将字段定义为可选?
【发布时间】:2021-10-11 13:17:35
【问题描述】:

GraphQL 允许您指定字段是否为可选(添加!

使用 Node API,我找不到该选项。

https://graphql.org/graphql-js/constructing-types/

我也经历了没有运气的类型

https://github.com/graphql/graphql-js/blob/main/src/index.ts

我错过了什么?

我基本上想要

type MyEntity {
  uuid: ID!
  name: String!
}

而不是

type MyEntity {
  uuid: ID
  name: String
}

【问题讨论】:

  • optional=nullable, required .... like graphqlNotNull( type )

标签: node.js graphql graphql-js


【解决方案1】:

我找到了答案

const preType:GraphQLOutputType = GraphQLInt;
// wrap the type with `GraphQLNonNull`
const type = new GraphQLNonNull(preType);

https://github.com/graphql/graphql-js/blob/main/src/type/definition.ts#L391

【讨论】:

    猜你喜欢
    • 2020-07-05
    • 2019-06-24
    • 2019-06-06
    • 2020-04-01
    • 2018-04-24
    • 2019-04-11
    • 2020-06-25
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多