I noticed the following note on GraphQL.org which seems to suggest that multiple mutations can be sent in a single request:
Multiple fields in mutations
A mutation can contain multiple fields, just like a query. There's one important distinction between queries and mutations, other than the name:
While query fields are executed in parallel, mutation fields run in series, one after the other.
This means that if we send two incrementCredits mutations in one request, the first is guaranteed to finish before the second begins, ensuring that we don't end up with a race condition with ourselves.
It doesn't look like this is currently supported by Apollo.. (unless it uses the non-standard query batching feature?) Are there any plans to introduce this?
You should be able to send multiple mutations like so:
mutation {
mutation1(input: { ... }) { ... }
mutation2(input: { ... }) { ... }
}
Let me know if that does not work for you because if it does not work then that is definitely a bug 馃憤
Ah thanks, that does work but is only really useful when you know ahead of time which mutations to bundle.
FWIW my use case is: I have an app with a datagrid and when a user edits a cell it calls the following mutation:
mutation UpdateActivity ($input: UpdateActivityInput!) {
updateActivity(input: $input) {
success
activity { ...activity }
}
}
That works great for a single cell edit but not so great for bulk edits, which currently call client.mutate
for each change; making hundreds of requests.
I was hoping I could merge these on the client rather than adding another mutation type to my server but it looks like this might not be possible after all 馃槥
It is possible with GraphQL, you just need to allow array arguments for your mutations.
At a conceptual level, you need to separate user action from server mutation request. A possible solution would be to store updates in a local store that you can diff for later updates to the server. The updates would seem real-time to the user and you would be able to adjust polling frequency to keep backend load to a minimum.
@jameswyse @calebmer I have the same issue and I wonder if you've found any "good" solution to share? I also want to avoid adding another mutation type to my server.
My issue is that I don't know exactly the number of mutations it'll execute. See this
@ignacio-chiazzo Here is an interesting idea. You can dynamically build a mutation query from the client that contains a separate field for each mutation you want to send. Just generate an alias for each field and I guess it should work as the field order is guaranteed within the same mutation query (as stated in the original post).
@ignacio-chiazzo You can send multiple mutations as a batch with apollo-link-batch-http.
Most helpful comment
Ah thanks, that does work but is only really useful when you know ahead of time which mutations to bundle.
FWIW my use case is: I have an app with a datagrid and when a user edits a cell it calls the following mutation:
That works great for a single cell edit but not so great for bulk edits, which currently call
client.mutate
for each change; making hundreds of requests.I was hoping I could merge these on the client rather than adding another mutation type to my server but it looks like this might not be possible after all 馃槥