0
0
GraphQLquery~10 mins

Optimistic UI updates in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Optimistic UI updates
User triggers mutation
UI immediately updates (optimistic)
Send mutation request to server
Server processes mutation
Confirm UI
When a user triggers a change, the UI updates right away (optimistic update) before the server confirms. Then the server response either confirms or rolls back the UI.
Execution Sample
GraphQL
mutation AddTodo {
  addTodo(text: "Buy milk") {
    id
    text
  }
}

# UI shows new todo immediately before server response
This mutation adds a todo item and the UI shows it immediately, assuming success.
Execution Table
StepActionUI StateServer ResponseResulting UI State
1User clicks 'Add Todo'No new todo shown yetNo request sent yetNo change
2UI adds todo optimisticallyNew todo 'Buy milk' shownRequest sent to serverNew todo visible
3Server processes mutationNew todo visibleServer returns success with id=101UI confirms todo with id=101
4User sees confirmed todoTodo with id=101 shownNo new responseUI stable
5If server failedNew todo visibleServer returns errorUI removes optimistic todo
💡 Execution ends when server response confirms or rejects the optimistic update
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
UI_TodoList[][{text: 'Buy milk', id: null}][{text: 'Buy milk', id: 101}][] if error, else unchanged
Key Moments - 3 Insights
Why does the UI show the new todo before the server responds?
The UI updates optimistically to give instant feedback, as shown in execution_table step 2, improving user experience.
What happens if the server returns an error?
The UI rolls back the optimistic update and removes the todo, as shown in execution_table step 5.
How does the UI know to replace the temporary todo with the confirmed one?
When the server responds with the new todo's id (step 3), the UI updates the todo item to include this id.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the UI state immediately after the user clicks 'Add Todo'?
ANew todo 'Buy milk' is shown optimistically
BNo new todo is shown yet
CTodo with id=101 is shown
DUI removes the todo
💡 Hint
Check execution_table row 1 under 'UI State'
At which step does the UI confirm the todo with the server-assigned id?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at execution_table row 3 under 'Resulting UI State'
If the server returns an error, what happens to the UI_TodoList variable?
AIt keeps the optimistic todo
BIt adds a duplicate todo
CIt removes the optimistic todo
DIt shows a loading spinner
💡 Hint
See variable_tracker after Step 5 and execution_table step 5
Concept Snapshot
Optimistic UI updates:
- UI updates immediately on user action
- Sends mutation to server in background
- On success, UI confirms update
- On failure, UI rolls back
- Improves user experience by reducing wait
Full Transcript
Optimistic UI updates happen when the user triggers a change, and the UI immediately shows the change before the server confirms. This makes the app feel faster. The mutation is sent to the server. If the server confirms success, the UI finalizes the change with server data like IDs. If the server returns an error, the UI removes the optimistic change to stay consistent. This flow helps users see instant feedback while keeping data accurate.