0

Is there a standard way to send a dynamic number of mutations in the same request with Apollo client ?

I have to deal with a Graphql API that only expose a single delete mutation, and I'd like to call it with multiple ids. Here's how it's defined:

mutation DeleteItemById($id: Int) {
    delete_item(id: $id) {
        id
    }
}

From what I read, I could do something like

mutation DeleteItemById($id_1: Int, $id_2: Int) {
    delete_item_1: delete_item(id: $id_1) {
        id
    }
    delete_item_2: delete_item(id: $id_2) {
        id
    }
}

But how could I generate such a query dynamically ? Is it a good practice anyway ? I always read it was not a good idea to dynamically generate graphql queries.

Plus, I'm using graphql-codegen and statically defining queries in .graphql files, so I imagine it will have trouble parsing dynamic ones.

Matthieu Dsprz
  • 385
  • 6
  • 10

1 Answers1

0

In general it's a bad idea to generate GraphQL queries dynamically. A good way to deal with this is to create a new mutation that supports multiple ids, validate and delete all in the same batch, like:

type Mutation {
  deleteItems(ids: [String!]!): Boolean!
}
  • Thank you for your answer, that's also what I'm familiar with in my GraphQL experience. That being said, in my current project I have little impact on the API side. Long story short the GraphQL API has been auto generated from Django Models, giving some CRUD functionnalities, and the backend team is insisting on the standard aspect of sending multiple mutations in one request. – Matthieu Dsprz Dec 09 '21 at 10:00