I am using react-apollo and have been for quite some time. One thing that has already been a problem for me is the fact that refetch doesn't work when using a mutation This has been a know issue for as long as I have been using the app.
I have got round this by using the refetch
prop that is available on a query.
<Query query={query} fetchPolicy={fetchPolicy} {...props}>
{({ loading, data, error, refetch }) => {
... pass down to mutation
</Query>
However I am now reading in the documentation that you recieve an update method as part of a mutation and you should use this to update your application after a mutation.
Can you use the update
function to update your UI's data and have it update after finishing a mutation? If you can, is this the standard way to do updates now?
*Using refetchQueries not working
As you can see in the image the console.info()
displays that the data.status = "CREATED";
but the request coming back from the mutation directly is data.status = "PICKED";
PICKED
is the correct and uptodate information in the DB.
In order of preference, your options are:
Do nothing. For regular updates to an individual node, as long as the mutation returns the mutated result, Apollo will update the cache automatically for you. When this fails to work as expected, it's usually because the query is missing the
id
(or_id
) field. When anid
field is not available, a custom dataIdFromObject function should be provided to the InMemoryCache constructor. Automatic cache updates also fail when people set theaddTypename
option tofalse
.Use
update
. Theupdate
function will run after your mutation completes, and lets you manipulate the cache directly. This is necessary if the mutation affects a field returning a list of nodes. Unlike simple updates, Apollo has no way to infer whether the list should be updated (and how) following the mutation, so we have to directly update the cache ourselves. This is typically necessary following create and delete mutations, but may also be needed after an update mutation if the updated node should be added or removed to some field that returns a list. The docs go into a good deal of detail explaining how to do this.refetchQueries
. Instead of updating the cache, you may also provide arefetchQueries
function, which should return an array of objects representing the queries to refetch. This is generally less desirable than usingupdate
since it requires one or more additional calls to the server. However, it may be necessary if the mutation does not return enough information to correctly update the cache manually. NOTE: The returned array may also be an array of strings representing operation names, though this is not well documented.Use
refetch
. As you already showed in your question, it's possible to use therefetch
function provided by aQuery
component inside yourMutation
component to refetch that specific query. This is fine if yourMutation
component is already nested inside theQuery
component, but generally usingrefetchQueries
will be a cleaner solution, particularly if multiple queries need to be refetched.Use
updateQueries
. This is a legacy option that's no longer well-documented, but provided similar functionality toupdate
beforeupdate
was added. It should not be used as it may be deprecated in the future.UPDATE:
You may also set up your schema in such a way that queries can be refetched as part of your mutation. See this article for more details.