我最近开始使用 GraphQL。我想一次以数组格式发送多封电子邮件,并在 postman 中发送请求(突变查询),如下所示。此查询适用于一次插入一条记录,但我想一次插入多封电子邮件和用户名。请给我任何如何查询的建议。谢谢。
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.',
}
})
});
更新联系代码,例如:
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}));
})
})
})
})
}
}
};
最佳答案
我还没有测试过这个。但它应该可以工作
突变 M {
第一: updateContacts(_id:"5603ce8abdc210e01834ec62",用户名:"nani",电子邮件:"nani@gmail.com")
{
我的联系方式 {
用户名,
电子邮件
}
},
第二: updateContacts(_id:"5603ce8abdc210e01834ec62",用户名:"nani",电子邮件:"nani@gmail.com")
{
我的联系方式{
用户名,
电子邮件
}
},
第三:updateContacts(_id:“5603ce8abdc210e01834ec62”,用户名:“nani”,电子邮件:“nani@gmail.com”)
{
我的联系方式{
用户名,
电子邮件
}
}
}
关于node.js - 如何使用 GraphQL 将多封电子邮件以数组格式发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32777796/