首页 > 解决方案 > graphql node api:如何将字段定义为可选?

问题描述

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
}

标签: node.jsgraphqlgraphql-js

解决方案


我找到了答案

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


推荐阅读