graphql node api:如何将字段定义为可选?

Posted

技术标签:

【中文标题】graphql node api:如何将字段定义为可选?【英文标题】:graphql node api: how can I define a field as optional? 【发布时间】: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 ) 【参考方案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

【讨论】:

以上是关于graphql node api:如何将字段定义为可选?的主要内容,如果未能解决你的问题,请参考以下文章