How to delete multiple items with GraphQL?

Mukhlis Kurnia Aji picture Mukhlis Kurnia Aji · Feb 9, 2017 · Viewed 7.7k times · Source

I am new in GraphQL. How can I write a delete mutation to delete multiple items (more than one ID) in GraphQL? I use scaphold.io.

Answer

marktani picture marktani · Feb 9, 2017

You can batch multiple mutations in the same request to the GraphQL server using GraphQL aliases.

Let's say this is how you delete one Item:

mutation deleteOne {
  deleteItem(id: "id1") {
    id
  }
}

Then this is how you can delete multiple items in one request:

mutation deleteMultiple {
  id1: deleteItem(id: "id1") {
    id
  }
  id2: deleteItem(id: "id2") {
    id
  }
  # ... and so on
  id100: deleteItem(id: "id100") {
    id
  }
}

It's helpful to know that multiple mutations in one request run sequentially in the stated order (from top to bottom). You can also run multiple queries in one request the same way, and multiple queries in one request run in parellel.

If you want to run say 1000 mutations, it might be better to batch them in 10 groups of 100.

More information and another example can be found in this FAQ article as well as the official GraphQL docs.