As explained in the mutations section, optimistic UI is a pattern that you can use to simulate the results of a mutation and update the UI even before receiving a response from the server. Once the response is received from the server, optimistic result is thrown away and replaced with the actual result.
Optimistic UI provides an easy way to make your UI respond much faster, while ensuring that the data becomes consistent with the actual response when it arrives.
Basic optimistic UI
Let's say we have an "edit comment" mutation, and we want the UI to update immediately when the user submits the mutation, instead of waiting for the server response. This is what the
optimisticResponse parameter to the
mutate method provides.
The main way to get GraphQL data into your UI components with Apollo is to use a query, so if we want our optimistic response to update the UI, we have to make sure to return an optimistic response that will update the correct query result.
Here's what this looks like in the code:
__typename because that's what our Type Policies use to determine a globally unique object ID. We need to make sure to provide the right values for those fields, so that Apollo knows what object we are referring to.
Adding to a list
In the example above, we showed how to seamlessly edit an existing object in the store with an optimistic mutation result. However, many mutations don't just update an existing object in the store, but they insert a new one.
In that case we need to specify how to integrate the new data into existing queries, and thus our UI. You can read in detail about how to do that in the article about controlling the store--in particular, we can use the
update method to insert a result into an existing query's result set.
update works exactly the same way for optimistic results and the real results returned from the server.
Here is a concrete example from GitHunt, which inserts a comment into an existing list of comments.