简体   繁体   中英

How to send multiple emails in array format to database using GraphQL

I am recently started working with GraphQL.I want to send multiple emails at a time in array format and sending the request(mutation query) in postman as below.This query is working for inserting one record at a time, but i want to insert multiple emails and usernames at a time.Please give me any suggestions how to query for that. Thank you.

 mutation M { 
updateContacts
  (
    _id:"5603ce8abdc210e01834ec62"
    username:"nani" 
   emails:"nani@gmail.com"
   ) 
{mycontacts{username,emails}}}

My GraphQL schema is:

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 code like:

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}));
         })
        })  
      })
    })
   }
  }
};

I haven't tested this. But it should work

mutation M { first: updateContacts(_id:"5603ce8abdc210e01834ec62", username:"nani", emails:"nani@gmail.com") { mycontacts { username, emails } }, second: updateContacts(_id:"5603ce8abdc210e01834ec62", username:"nani", emails:"nani@gmail.com") { mycontacts{ username, emails } }, third: updateContacts(_id:"5603ce8abdc210e01834ec62", username:"nani", emails:"nani@gmail.com") { mycontacts{ username, emails } } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM