Should `refetch` force network only policy? (apollo-client)
See original GitHub issueApollo Client version: 2.6.4
, client built in React, using hooks.
I was hoping that I could use a refetch
(returned from useQuery
) to pull only from the cache, but from debugging and looking through the code (https://github.com/apollographql/apollo-client/blob/c44e8211268e808106e81da7665f65f81fac2baf/packages/apollo-client/src/core/ObservableQuery.ts#L307) it seems like the fetchpolicy favours network calls?
Here’s the reason I want to allow pulling from the cache only in a refetch:
The query organization
has a field for a list of users (of type User
).
The client (React) calls the mutation addUser
which has a return type Organization
This response from the mutation already updates the cache of the organization with the new list of users.
refetch
is called (in the useQuery
hook) so that the Component that renders the list is updated with the new user. The user is already in the cache, the mutation response updated it, and I see that it is complete
in debugging. Yet it still does a network call.
Perhaps I’ve misunderstood the point of refetch
(I want my useQuery
hook to return new data to re-render the component) - or perhaps there is a way to have the refetch honor the cache? Please help
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:9 (1 by maintainers)
Top GitHub Comments
If the user wants to refresh data, it should force to get recent data to the user, not from the caches. The subscription design with the caches and updating is fine but we need to
network-only
for the refetch.It should at least be an option.
Something like this
.refetch({}, { fetchPolicy: 'network-only' })
.With the current behaviour we cannot simply do
await refetch()
if we actually want to wait for the network results, because it already resolves when it finds something from the cache.