What is the difference between a query, a mutation, and a subscription in GraphQL?
Learn the difference between GraphQL queries, mutations, and subscriptions — reading data, changing data, and streaming real-time updates.
Expected Interview Answer
In GraphQL, a query reads data, a mutation writes or changes data, and a subscription streams real-time updates to the client over a persistent connection — they are the three root operation types.
Queries are read-only and can be executed in parallel since they have no side effects. Mutations are for creating, updating, or deleting data and are executed in series to avoid race conditions, typically returning the affected data so the client can update its cache. Subscriptions keep a long-lived connection (usually WebSockets) open so the server can push new data to the client whenever a subscribed event occurs, rather than the client polling repeatedly.
- Clear separation of reads, writes, and real-time streams
- Queries run in parallel; mutations run serially for safety
- Mutations can return updated data to sync the client
- Subscriptions replace inefficient polling with server push
- All three share the same schema and type system
AI Mentor Explanation
A query is like checking the current scoreboard — you read the total without changing it. A mutation is like the umpire signaling a boundary, actually altering the score. A subscription is like a live commentary feed piped to your ear that pushes each new run the instant it happens, instead of you glancing up at the board every few seconds.
Step-by-Step Explanation
Step 1
Use a query to read
Send a query operation to fetch data; it is side-effect free and can run in parallel with other queries.
Step 2
Use a mutation to write
Send a mutation to create, update, or delete data; mutations execute serially to prevent race conditions.
Step 3
Return affected data
Have mutations return the changed object so the client can refresh its local cache without a separate query.
Step 4
Open a subscription
Subscribe to an event; the server holds a persistent connection and pushes new data when the event fires.
Step 5
Handle the stream
The client processes each pushed payload as it arrives, updating the UI in real time until it unsubscribes.
What Interviewer Expects
- Correct mapping: query=read, mutation=write, subscription=real-time
- Knowledge that queries run in parallel but mutations run in series
- Understanding that mutations usually return the changed data
- Awareness subscriptions use a persistent transport like WebSockets
- Recognition that all three are declared as root types in the schema
Common Mistakes
- Using a query to change data instead of a mutation
- Assuming mutations run in parallel like queries
- Thinking subscriptions work over standard request-response HTTP
- Forgetting mutations can and should return the updated object
- Confusing subscriptions with client-side polling
Best Answer (HR Friendly)
“In GraphQL, a query reads information, a mutation changes information, and a subscription keeps a live connection open so the server can push updates as they happen. It is the difference between looking something up, editing it, and getting real-time alerts.”
Code Example
query GetUser {
user(id: "42") {
name
email
}
}
mutation AddPost {
createPost(title: "Hello") {
id
title
}
}
subscription OnNewPost {
postCreated {
id
title
}
}Follow-up Questions
- Why do mutations execute serially while queries run in parallel?
- What transport is typically used for subscriptions?
- Why should a mutation return the affected data?
- When would you choose polling over a subscription?
- Are all three operation types required in a schema?
MCQ Practice
1. Which GraphQL operation is used to change server-side data?
Mutations are the operation type for creating, updating, or deleting data; queries are read-only.
2. How are top-level mutation fields executed relative to each other?
Top-level mutation fields run in series to avoid race conditions, whereas query fields can be resolved in parallel.
Flash Cards
Query vs mutation vs subscription — Query reads data, mutation changes data, subscription streams real-time updates over a persistent connection.
How do mutations execute? — Top-level mutation fields run serially to prevent race conditions; query fields can run in parallel.
What transport do subscriptions use? — Usually WebSockets (a persistent connection) so the server can push data to the client as events occur.
Why do mutations return data? — Returning the affected object lets the client update its cache without issuing a separate query.