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