GraphQL mutator,带有必需的args

时间:2018-06-15 17:40:25

标签: 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,
      },
    }),
  }),
});

我希望确保mutatePerson仅在nameschool参数都存在的情况下才有效。我怎么检查呢?

2 个答案:

答案 0 :(得分:3)

$loop->iteration类型包装器用于将字段和参数指定为非null。对于字段,这意味着查询结果中字段的值不能为空。对于参数,这意味着不能省略参数或具有null值。所以你的代码需要看起来更像这样:

GraphQLNonNull

答案 1 :(得分:1)

如果要拒绝诸如“”之类的空字符串,最好按以下步骤进行操作(在模型中),它不同于Null,这样您就可以同时拒绝Null和空的“”。

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const buinessSchema = new Schema({
  name: { type: String, required: true, unique: true },
  address: { type: String, required: true },
  phone: { type: String, required: true },
  email: { type: String, required: true },
  website: { type: String, required: true },
  currency: { type: String, required: true },
  aboutus: { type: String, required: true },
  terms: String,
  slogan: String,
  logo: { type: String, required: true }
});

module.exports = mongoose.model("Buiness", buinessSchema);