具有必需参数的GraphQL mutator

mba*_*ski 1 graphql graphql-js

我正在为GraphQL模式编写一个变体:

const Schema = new GraphQLSchema({
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: () => ({
      person: {
        type: GraphQLString,
        args: {
          name: {type: GraphQLString},
          school: {type: GraphQLString},
        },
        resolve: mutatePerson,
      },
    }),
  }),
});
Run Code Online (Sandbox Code Playgroud)

我想确保mutatePerson只有同时存在name和school参数时,该方法才能起作用。我该如何检查?

Dan*_*den 5

该GraphQLNonNull类型的包装用于指定两个字段和参数非空。对于字段,这意味着查询结果中的字段值不能为null。对于参数,这意味着不能忽略该参数或将其值设置为null。因此,您的代码只需要看起来像这样:

args: {
  name: {
    type: new GraphQLNonNull(GraphQLString),
  },
  school: {
    type: new GraphQLNonNull(GraphQLString),
  },
},
Run Code Online (Sandbox Code Playgroud)