Optimistic UI updates make your app feel faster by showing changes right away before the server confirms them.
Optimistic UI updates in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
GraphQL
mutation UpdateItem($id: ID!, $newValue: String!) { updateItem(id: $id, value: $newValue) { id value } } // Use optimisticResponse in your client code like this: client.mutate({ mutation: UpdateItem, variables: { id: "1", newValue: "New Text" }, optimisticResponse: { updateItem: { id: "1", value: "New Text", __typename: "Item" } } })
The optimisticResponse mimics the server response to update the UI immediately.
Make sure the optimistic response matches the shape of the real response.
Examples
GraphQL
client.mutate({
mutation: LIKE_POST,
variables: { postId: "123" },
optimisticResponse: {
likePost: {
id: "123",
likes: 101,
__typename: "Post"
}
}
})GraphQL
client.mutate({
mutation: DELETE_COMMENT,
variables: { commentId: "456" },
optimisticResponse: {
deleteComment: {
id: "456",
__typename: "Comment"
}
},
update(cache) {
cache.modify({
fields: {
comments(existingComments = [], { readField }) {
return existingComments.filter(
commentRef => readField('id', commentRef) !== "456"
);
}
}
});
}
})Sample Program
This mutation adds a new task and shows it immediately in the UI with a temporary id before the server responds.
GraphQL
mutation AddTask($text: String!) { addTask(text: $text) { id text completed } } // Client code example: client.mutate({ mutation: AddTask, variables: { text: "Buy milk" }, optimisticResponse: { addTask: { id: "temp-id-1", text: "Buy milk", completed: false, __typename: "Task" } } })
Important Notes
If the server later returns an error, you should handle reverting the optimistic update.
Optimistic updates improve user experience by reducing waiting time.
Summary
Optimistic UI updates show changes instantly before server confirmation.
Use optimisticResponse to tell the client what to show.
Make sure to handle errors to keep UI consistent.
Practice
1. What is the main purpose of using
optimisticResponse in GraphQL client updates?easy
Solution
Step 1: Understand optimisticResponse role
TheoptimisticResponseis used to update the UI instantly, assuming the server will succeed.Step 2: Compare options with purpose
Only To show UI changes immediately before the server responds describes showing UI changes immediately before server confirmation, which matches the optimistic update concept.Final Answer:
To show UI changes immediately before the server responds -> Option AQuick Check:
optimisticResponse = immediate UI update [OK]
Hint: Think: optimistic means 'hopeful' update shown early [OK]
Common Mistakes:
- Confusing optimisticResponse with delayed updates
- Thinking it rolls back changes automatically
- Assuming it fetches data without UI change
2. Which of the following is the correct syntax to provide an optimistic response in a GraphQL mutation using Apollo Client?
easy
Solution
Step 1: Recall optimisticResponse structure
The optimisticResponse must include the__typenamefield to match the GraphQL schema type.Step 2: Check each option for __typename
Only mutation({ variables, optimisticResponse: { __typename: 'User', id: 1, name: 'Test' } }) includes__typename: 'User'along with id and name, making it syntactically correct.Final Answer:
mutation({ variables, optimisticResponse: { __typename: 'User', id: 1, name: 'Test' } }) -> Option BQuick Check:
Include __typename in optimisticResponse [OK]
Hint: Always add __typename in optimisticResponse object [OK]
Common Mistakes:
- Omitting __typename causes errors
- Using 'optimistic' instead of 'optimisticResponse'
- Providing incomplete optimisticResponse data
3. Given this mutation call with optimisticResponse:
client.mutate({
mutation: ADD_TODO,
variables: { text: 'Buy milk' },
optimisticResponse: {
__typename: 'Mutation',
addTodo: {
__typename: 'Todo',
id: 'temp-id',
text: 'Buy milk',
completed: false
}
}
})
What will the UI show immediately after this mutation is called but before the server responds?medium
Solution
Step 1: Analyze optimisticResponse content
The optimisticResponse provides a new todo with id 'temp-id', text 'Buy milk', and completed false.Step 2: Understand UI behavior
The UI will immediately show this new todo item with the given fields before server confirmation.Final Answer:
A new todo with id 'temp-id' and text 'Buy milk' shown -> Option CQuick Check:
optimisticResponse shows temporary UI data [OK]
Hint: optimisticResponse data appears instantly in UI [OK]
Common Mistakes:
- Expecting no UI change before server response
- Thinking optimisticResponse causes errors
- Assuming blank or incomplete UI display
4. You wrote this optimisticResponse but the UI does not update immediately:
optimisticResponse: {
addTodo: {
id: 'temp-id',
text: 'Buy milk',
completed: false
}
}
What is the most likely reason for this issue?medium
Solution
Step 1: Check optimisticResponse structure
The optimisticResponse must include__typenamefor each object to match the schema.Step 2: Identify missing fields
The given optimisticResponse lacks__typenamefields, causing Apollo Client to ignore it.Final Answer:
Missing __typename fields in optimisticResponse -> Option DQuick Check:
__typename required for optimisticResponse to work [OK]
Hint: Always add __typename to every object in optimisticResponse [OK]
Common Mistakes:
- Forgetting __typename causes no UI update
- Assuming variables affect optimisticResponse directly
- Thinking optimisticResponse must be a function
5. You want to implement an optimistic UI update for a mutation that toggles a user's 'active' status. The server might reject the change. Which approach best ensures UI consistency?
hard
Solution
Step 1: Understand optimistic UI with possible server rejection
Optimistic UI shows changes immediately but must handle errors to keep UI correct.Step 2: Evaluate options for best practice
Use optimisticResponse to toggle status immediately and handle errors to revert if needed uses optimisticResponse for instant UI update and error handling to revert if server rejects, ensuring consistency.Final Answer:
Use optimisticResponse to toggle status immediately and handle errors to revert if needed -> Option AQuick Check:
Optimistic update + error handling = consistent UI [OK]
Hint: Combine optimisticResponse with error handling for safe UI [OK]
Common Mistakes:
- Ignoring error handling causes UI mismatch
- Waiting for server loses optimistic UI benefits
- Assuming optimisticResponse alone guarantees correctness
