0

我正在学习 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 的数组的任何想法?如果您需要我当前代码库中的任何其他内容,请告诉我。

4

1 回答 1

0

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

new GraphQLList(GraphQLID)

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

new GraphQLList(new GraphQLNonNull(GraphQLID))

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

new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID)))
于 2020-02-06T23:49:59.293 回答