node.js - 如何使用 GraphQL 将多封电子邮件以数组格式发送到数据库

标签 node.js mongodb es6-promise graphql

我最近开始使用 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/

相关文章:

javascript - 如何在 promise 解决时处理 promise ?

javascript - 为什么所有的异步函数都完成后 Promise.all 就不能工作了?

javascript - Heroku Foreman 在 Express.js bodyParser() 调用上退出

node.js - MongoDB $lookup Objectid 得到空数组?

node.js - 使用 TypeScript 配置 React Native 时出错

node.js - 近场必须是点

node.js - 通过 Node.js 从 MongoDB 返回大量项目

javascript - "event loop queue"和 "job queue"和有什么区别?

node.js - Cassandra +nodejs : how to get the Keyspaces and Tables?

javascript - 在 Node.js 中使用 Place API 库的地点详细信息