Mongoose 将ID数组添加到GraphQL变体
我正在学习GraphQL,并为最终用户构建一个应用程序,以输入保险计划,并让他们所在地区的医生接受他们选择的保险。我正试图将一系列“docId”和“insin”推到我的突变中,这样每个医生和每个计划都有一系列被接受的计划和医生接受的计划 以下是突变设置:Mongoose 将ID数组添加到GraphQL变体,mongoose,graphql,graphql-mutation,Mongoose,Graphql,Graphql Mutation,我正在学习GraphQL,并为最终用户构建一个应用程序,以输入保险计划,并让他们所在地区的医生接受他们选择的保险。我正试图将一系列“docId”和“insin”推到我的突变中,这样每个医生和每个计划都有一系列被接受的计划和医生接受的计划 以下是突变设置: // Must find a way to add an array of 'docId's and 'insId's // to their respective mutations. const Mutation = new GraphQL
// 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);
尝试在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的数组,有什么想法吗?如果您需要我当前代码库中的任何其他内容,请告诉我 在GraphQL中,List是一种包装类型,它包装另一种类型,并表示所包装类型的列表或数组。因此字符串列表([String]
)将表示一个字符串数组。如果您的参数包含ID列表,那么您的参数类型应该是[ID]
。通过编程,我们将其编写为:
new GraphQLList(GraphQLID)
您可能还希望防止在提供的列表中包含null,在这种情况下,我们将执行以下操作:
new GraphQLList(new GraphQLNonNull(GraphQLID))
或者如果整个参数不应为null:
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID)))
正是我需要的。我想我知道我可能必须使用GraphQllist,但不确定它是否像包装GraphQLID那样简单。非常感谢。
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID)))