Race-condition in refetchQueries (cache-first query completes before network-only query)
See original GitHub issueIntended outcome:
Using refetchQueries
on a mutation should guarantee to update the store from the network query result.
Actual outcome:
If the application triggers the same query, that was supposed to be refetched, shortly after the mutation has run (which by default will happen with the cache-first
policy), this query will typically hit the cache and complete before the network-only
query issued by refetchQueries
will finish.
Since the refetch query has a lower requestId
its result will not be propagated to the store, see: https://github.com/apollographql/apollo-client/blob/d420cbdbb3e6637ee2e8e8c211e3d28850b93c6e/src/queries/store.ts#L135
How to reproduce the issue:
See: https://github.com/ctavan/react-apollo-error-template/tree/reproduce-refetch-query-race
I think the issue is best explained when looking at the demo app / screenshot:
The steps to reproduce are:
- Load the page -> List query is run and populates the store with 3 people.
- Go to “create” which unmounts the List and mounts the Create component. Clicking the button will fire a mutation which adds a new person.
- The mutation has
refetchQueries: ['People'],
specified.
- As is visible from the console output which simulates the server behavior, apollo-client starts a network request to refetch the query right after the mutation network request has resolved (and even before the
mutate()
promise resolves, as indicated byMutation done
). In the example this getslastRequestId = 4
:
- At this point the success callback of the resolved mutation redirects back to the
List
component which re-triggers the list-query there,lastRequestId = 5
:
- Shortly after that, the network request for the refetch resolves and as can be seen from the server output there were actually 4 elements returned.
- However the store still only holds 3 elements for the
people
query as can be seen from the Apollo client dev toolbar. I assume this is because thecache-first
which was executed synchronously resolved before thenetwork-only
refetch query and thenetwork-only
query was therefore discarded.
My assumption would be that refetchQueries
guarantees to sync the current store with the result returned from the server and that intermediate cache-first
queries cannot interfere with this.
So, is this a bug or a feature? 😃
Issue Analytics
- State:
- Created 6 years ago
- Reactions:11
- Comments:24 (3 by maintainers)
Top GitHub Comments
The issue still persists and I believe this issue should not be closed.
This issue has been automatically marked as stale becuase it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client!