Multiple Mutations in One Request
You'll learn: How to bundle several mutations together in a single GraphQL request and work with their combined responses.
Why Send Multiple Mutations Together?
In REST APIs, you'd need separate HTTP requests for each create, update, or delete operation. GraphQL lets you batch multiple mutations into one network round-trip, reducing latency and keeping related changes together.
Think of it like a restaurant order: instead of making three trips to the counter (one for your burger, one for fries, one for a drink), you place the entire order at once and get everything together.
How It Works
GraphQL executes mutations sequentially in the order you write them—not in parallel like queries. This guarantees predictable behavior when one mutation depends on another's success.
mutation MultipleUpdates($userId: ID!, $postId: ID!) {
updateUser(id: $userId, name: "Alice") {
id
name
}
deletePost(id: $postId) {
success
}
createComment(postId: $postId, text: "Great!") {
id
text
}
}
Each mutation gets its own result in the response object:
{
"data": {
"updateUser": { "id": "1", "name": "Alice" },
"deletePost": { "success": true },
"createComment": { "id": "99", "text": "Great!" }
}
}
Use With Care
While convenient, be cautious: if mutation #2 fails, you may have already committed mutation #1. GraphQL doesn't automatically roll back completed mutations. Design your mutations to be safe or handle partial failures gracefully in your client code.
Key Takeaway: GraphQL executes multiple mutations sequentially in one request, reducing network overhead but requiring careful error handling since there's no automatic rollback.