Optimistic UI updates in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using optimistic UI updates, we want to know how the work grows as more data changes happen.
How does the system handle many updates quickly and what costs come with it?
Analyze the time complexity of this optimistic update GraphQL mutation.
mutation AddComment($postId: ID!, $text: String!) {
addComment(postId: $postId, text: $text) {
id
text
author {
id
name
}
}
}
This mutation adds a comment and the UI updates immediately before server response.
Look for repeated work when many optimistic updates happen.
- Primary operation: Updating the local cache or UI state for each new comment.
- How many times: Once per comment added optimistically, so it grows with the number of comments.
Each new comment triggers a cache update and UI refresh.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 cache updates and UI refreshes |
| 100 | 100 cache updates and UI refreshes |
| 1000 | 1000 cache updates and UI refreshes |
Pattern observation: The work grows directly with the number of optimistic updates.
Time Complexity: O(n)
This means the time to update the UI grows in a straight line as more updates happen.
[X] Wrong: "Optimistic updates happen instantly with no cost no matter how many."
[OK] Correct: Each update changes the UI and cache, so more updates mean more work and time.
Understanding how optimistic UI updates scale shows you can balance fast user feedback with system performance.
What if we batch multiple optimistic updates together? How would the time complexity change?
Practice
optimisticResponse in GraphQL client updates?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]
- Confusing optimisticResponse with delayed updates
- Thinking it rolls back changes automatically
- Assuming it fetches data without UI change
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]
- Omitting __typename causes errors
- Using 'optimistic' instead of 'optimisticResponse'
- Providing incomplete optimisticResponse data
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?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]
- Expecting no UI change before server response
- Thinking optimisticResponse causes errors
- Assuming blank or incomplete UI display
optimisticResponse: {
addTodo: {
id: 'temp-id',
text: 'Buy milk',
completed: false
}
}
What is the most likely reason for this issue?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]
- Forgetting __typename causes no UI update
- Assuming variables affect optimisticResponse directly
- Thinking optimisticResponse must be a function
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]
- Ignoring error handling causes UI mismatch
- Waiting for server loses optimistic UI benefits
- Assuming optimisticResponse alone guarantees correctness
