Q4 of 20 · GraphQL
What is the difference between a query and a mutation, and how do you test each?
Short answer
Short answer: Queries read data; mutations change it. Test queries for field selection, nesting and nullability. Test mutations for both the returned payload AND the persisted state, since a mutation can return a success payload while the write fails downstream.
Detail
Queries are reads — by convention free of side effects. Mutations are writes — they create, update or delete and then return a payload describing the result.
Testing a query: verify the requested fields come back correctly shaped, nesting resolves, nullability is honoured, and pagination/filtering arguments work.
Testing a mutation needs an extra step:
- Run the mutation and assert on its return payload (status field, returned object, errors).
- Run a follow-up query to confirm the change actually persisted.
The reason for step 2: a mutation resolver can return an optimistic success payload while the underlying write silently fails, or partially applies. Only a read-back proves the state changed. This is the GraphQL equivalent of the REST habit of GET-after-POST.
Also test mutation idempotency where it matters (e.g. a "create" that's accidentally run twice) and the error path (invalid input rejected with a useful code).
// WHAT INTERVIEWERS LOOK FOR
// COMMON PITFALL
// Related questions