Santy
Santy

Reputation: 11

How can I save multiple rows in one mutation in graphql and mongodb?

Here I want to add multiple thoughts in one mutation query. How can I achieve this:

mutation{
  savethoughts(data: [{id:1,name:"a"},data: {id:2,name:"b"}]){
    id
  }
}

Upvotes: 1

Views: 2952

Answers (1)

Ashok
Ashok

Reputation: 2932

For save multiple records in database you need to follow every steps carefully

For example

mutation{
  setMultipleRecord(data: [{title: "first Title", name: "First"},
                           {title: "Second Title", name: "Second"}])
}

You must have schema type

  `
  .......
  input Record {
    title: String!
    name: String!
  }
  .....
  ` 

And add Mutation as given that

  type Mutation/type RootMutation { {
    ......
    setMultipleRecord(data: [Record]): String!
  }

Here you can see Record is input type schema object where is data parameter holding variable which we are using to get data object.

you can give any name instead of data also change in the argument in mutation

Now in resolver function a

  Mutation: {
    setMultipleRecord: async(args, { data }) => {
      console.log(data)
      //Here data contain data which you passed which is
      // [{title: "first Title", name: "First"},
      //  {title: "Second Title", name: "Second"}]
      // Perform operation as your requirement
      // Please return String 
    }
  }

Now you can change name as par your requirement of object.

You made it successfully...

Upvotes: 2

Related Questions