【问题标题】:Add an array of IDs to a GraphQL mutation将一组 ID 添加到 GraphQL 突变
【发布时间】:2020-05-23 00:35:24
【问题描述】:

我正在学习 GraphQL 并构建一个应用程序,供最终用户输入保险计划并返回他们所在地区接受他们选择的保险的医生。我正在尝试将一组“docId”和“insId”推送到我的突变中,以便每个医生和每个计划分别有一系列接受的计划和接受的医生。

这是突变设置:

// Must find a way to add an array of 'docId's and 'insId's
// to their respective mutations.
const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        addDoctor: {
            type: DoctorType,
            args: {
                doctorName: { type: GraphQLString },
                city: { type: GraphQLString },
                specialty: { type: GraphQLString },
                insId: { type: GraphQLID }
            },
            resolve(parent, args){
                let doctor = new Doctor({
                    doctorName: args.doctorName,
                    city: args.city,
                    specialty: args.specialty,
                    insId: [args.insId]
                });
                return doctor.save();
            }
        },
        addPlan: {
            type: InsuranceType,
            args: {
                insName: { type: GraphQLString },
                usualCoPay: { type: GraphQLString },
                docId: { type: GraphQLID }
            },
            resolve(parent, args){
                let plan = new Plan({
                    insName: args.insName,
                    usualCoPay: args.usualCoPay,
                    docId: [args.docId]
                });
                return plan.save();
            }
        }
    }
})

这里是猫鼬模型:

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

const doctorSchema = new Schema({
    doctorName: String,
    city: String, 
    specialty: String,
    insId: Array
})

module.exports = mongoose.model('Doctor', doctorSchema);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const planSchema = new Schema({
    insName: String,
    usualCoPay: String,
    docId: Array
});

module.exports = mongoose.model('Plan', planSchema);

当尝试像这样在 GraphiQL 中添加每个 id 的数组时:

mutation {
    addPlan(insName:"United", usualCoPay: "$30", docId: ["5e37548b42037513e5dfc790", "5e37544642037513e5dfc78f", "5e3754b842037513e5dfc791"]){
            insName
        usualCoPay
    }
  }

我来了

{
  "errors": [
    {
      "message": "Expected type ID, found [\"5e37548b42037513e5dfc790\", \"5e37544642037513e5dfc78f\", \"5e3754b842037513e5dfc791\"].",
      "locations": [
        {
          "line": 2,
          "column": 57
        }
      ]
    }
  ]
}

关于我需要更改什么以确保我能够放入每个 ID 的数组的任何想法?如果您需要我当前代码库中的其他内容,请告诉我。

【问题讨论】:

    标签: mongoose graphql graphql-mutation


    【解决方案1】:

    在 GraphQL 中,List 是一个 wrapping 类型,它包装了另一种类型,并表示它所包装类型的列表或数组。因此,字符串列表 ([String]) 将表示字符串数组。如果您的参数采用 ID 列表,那么您的参数类型应该是 [ID]。以编程方式,我们将其写为:

    new GraphQLList(GraphQLID)
    

    您可能还希望防止空值包含在提供的列表中,在这种情况下,我们会这样做:

    new GraphQLList(new GraphQLNonNull(GraphQLID))
    

    或者如果整个参数永远不应该为空:

    new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID)))
    

    【讨论】:

    • 正是我需要的。我想我明白我可能不得不使用 GraphQLLList 但不确定它是否像用它包装 GraphQLID 一样简单。谢谢!
    猜你喜欢
    • 2020-11-19
    • 1970-01-01
    • 2018-08-08
    • 2017-05-05
    • 2016-05-06
    • 1970-01-01
    • 2020-09-23
    • 2019-12-18
    • 1970-01-01
    相关资源
    最近更新 更多