I want to use optimistic UI updates after a mutation: https://www.apollographql.com/docs/react/basics/mutations.html
Im confused about the relationship between 'optimisticResponse' and 'update'.
Here optimisticResponse is used:
const CommentPageWithData = graphql(submitComment, {
props: ({ ownProps, mutate }) => ({
submit: ({ repoFullName, commentContent }) => mutate({
variables: { repoFullName, commentContent },
optimisticResponse: {
__typename: 'Mutation',
submitComment: {
__typename: 'Comment',
// Note that we can access the props of the container at `ownProps` if we
// need that information to compute the optimistic response
postedBy: ownProps.currentUser,
createdAt: +new Date,
content: commentContent,
},
},
}),
}),
})(CommentPage);
Will just using this update the store?
The documentation then says this is used to update the cache:
const text = 'Hello, world!';
client.mutate({
mutation: TodoCreateMutation,
variables: {
text,
},
update: (proxy, { data: { createTodo } }) => {
// Read the data from our cache for this query.
const data = proxy.readQuery({ query: TodoAppQuery });
// Add our todo from the mutation to the end.
data.todos.push(createTodo);
// Write our data back to the cache.
proxy.writeQuery({ query: TodoAppQuery, data });
},
});
This is what I've used to successfully update the UI without using the optimisticResponse function.
What is the difference between the two? Should you use both or just one?
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, the optimistic result is thrown away and replaced with the actual result.
If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns. To do so, the mutation must return the id of the modified entity, along with the values of the fields that were modified.
Update mutations let you update existing objects of a particular type. With update mutations, you can filter nodes and set or remove any field belonging to a type.
To expand on the other two answers, the distinction lies in whether or not the thing you are 'updating' already exists in the cache or not.
As per the docs, if you are updating an existing item, say editing a title of a todo item, you only need optimisticResponse
. Why? because the cache contains the node, and you only need to tell it that something new happened with the new node, which is immediately reflected on the UI.
optimisticResponse
just provides an 'immediate' result data from a mutation.
Now we have a second case, you want to add a new Todo item to the list. First, the cache needs to know that a new item is created. As soon as you provide the update
attribute to a Mutation, you are taking control of the caches' state.
update
takes place ofrefetchQueries
, which means you are in control of the cache state.
With update
you can reach into the cache and modify/append exclusively the node you need, as opposed to maybe refetching an entire hierarchy of data. However, you are still waiting for the Mutation to finish. If you provide the update
along side the optimisticResponse
, you are providing an immediate assumed response, and giving it to your personal update
function, which then instantly updates the cache.
The reason these two are paired in scenario two, is that you are completely bypassing the server responses. If you just give a 'immediate' response, Apollo is still in the mode where it waits for the server to update cache. update
lets you highjack that as well, and do it client side.
Final Note: you are assuming the server is always responding without errors. Error handling elsewhere will still work, but you might get into UI inconsistencies if you are frequently catching errors (say
isLoggedIn
scenarios) so def make sure that the queries you 'fast track' are typically healthy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With