我最近开始使用 GraphQL。我想一次以数组格式发送多封电子邮件,并在邮递员中发送请求(突变查询),如下所示。此查询用于一次插入一条记录,但我想插入多个电子邮件和用户名一次。请给我任何建议如何查询。谢谢你。
mutation M {
updateContacts
(
_id:"5603ce8abdc210e01834ec62"
username:"nani"
emails:"nani@gmail.com"
)
{mycontacts{username,emails}}}
我的 GraphQL 架构是:
export default new GraphQLObjectType({
name: 'User',
description: 'A user',
fields: () => ({
_id: {
type: new GraphQLNonNull(GraphQLID)
},
mycontacts:{
type: new GraphQLList(MyContactType)
}
})
});
export var MyContactType = new GraphQLObjectType({
name: 'MyContact',
description: 'A mycontact',
fields: () => ({
username:{
type: GraphQLString,
description: 'The username.',
},
emails:{
type: new GraphQLList(GraphQLString),
description: 'The emails.',
}
})
});
updateContact 代码如:
updateContacts:{
type: UserType,
args: {
_id: {
type:GraphQLString,
description: 'The _id of the user.',
},
mycontacts:{
type: new GraphQLList(GraphQLString)
},
username:{
type: GraphQLString,
description: 'The username.',
},
emails:{
type: new GraphQLList(GraphQLString),
description: 'The emails.',
}
},
resolve: (root,{username,mycontacts,emails,_id}) =>{
return new Promise((resolve, reject) => {
User.findOne({_id:_id},(err,res)=>{
var data={'username':username,'emails':emails};
_.each({data}, function (contact) {
res.mycontacts.push(contact);
User.update({_id}, {$set:{mycontacts:res.mycontacts}}, (err,result) => {
err ? reject(err) :resolve(User.findOne({'_id':_id}));
})
})
})
})
}
}
};