Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Optimistic UI Updates with GraphQL
📖 Scenario: You are building a simple task management app where users can add tasks. To make the app feel fast, you want to show new tasks immediately in the UI before the server confirms the addition. This technique is called optimistic UI updates.
🎯 Goal: Create a GraphQL mutation setup that includes an optimistic response to update the UI instantly when adding a new task.
📋 What You'll Learn
Create a GraphQL mutation called ADD_TASK that adds a task with id and title fields.
Define a variable called newTask with id and title values.
Use an optimisticResponse object that matches the mutation shape with the newTask data.
Include the update function to update the cache with the new task.
💡 Why This Matters
🌍 Real World
Optimistic UI updates make apps feel faster by showing changes immediately without waiting for server confirmation.
💼 Career
Understanding optimistic updates is important for frontend developers working with GraphQL and Apollo Client to improve user experience.
Progress0 / 4 steps
1
Define the GraphQL mutation
Create a GraphQL mutation called ADD_TASK using the gql tag. The mutation should be named addTask and accept a title argument of type String!. It should return the id and title of the added task.
GraphQL
Hint
Use the gql tag to define a mutation named addTask that takes a title argument and returns id and title.
2
Create the new task variable
Create a constant variable called newTask with an id of "temp-id-1" and a title of "Learn optimistic UI".
GraphQL
Hint
Define newTask as an object with the exact id and title values.
3
Add the optimisticResponse object
Create a constant called optimisticResponse that matches the mutation shape. It should have an addTask field containing the newTask object.
GraphQL
Hint
The optimisticResponse object should have a field named addTask set to newTask.
4
Write the update function to modify the cache
Write a function called update that takes cache and { data: { addTask } } as parameters. Inside, call cache.modify to add addTask to the tasks field in the cache.
GraphQL
Hint
Use cache.modify to update the tasks field by appending the new task reference created with cache.writeFragment.
Practice
(1/5)
1. What is the main purpose of using optimisticResponse in GraphQL client updates?
easy
A. To show UI changes immediately before the server responds
B. To delay UI updates until the server confirms
C. To rollback UI changes after server response
D. To fetch data from the server without updating UI
Solution
Step 1: Understand optimisticResponse role
The optimisticResponse is 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 A
Quick 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?
D. mutation({ variables, optimisticResponse: { id: 1 } })
Solution
Step 1: Recall optimisticResponse structure
The optimisticResponse must include the __typename field 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.
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
A. Use optimisticResponse to toggle status immediately and handle errors to revert if needed
B. Wait for server response before updating UI to avoid inconsistencies
C. Update UI without optimisticResponse and ignore server errors
D. Use optimisticResponse but do not handle errors, assuming success
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 A