apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

Should `refetch` force network only policy? (apollo-client)

Open ajdaniel opened this issue 5 years ago • 7 comments

Apollo 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

ajdaniel avatar Oct 28 '19 11:10 ajdaniel

Shouldn't the query be updated automatically if its data in the cache is updated, because it's observable?

micimize avatar Feb 14 '20 17:02 micimize

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.

hyochan avatar Apr 15 '20 08:04 hyochan

any update on this? @benjamn

kunalvishnoi avatar Oct 21 '20 12:10 kunalvishnoi

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.

tafelnl avatar May 05 '21 10:05 tafelnl

Another parameter for refetch, where you could override more properties of useQuery (including fetchPolicy) is much needed. It would be also useful in react-native apps, where this could be used on pull-down to refresh action. Here user expects new data instead of stale cache data.

nkovacic avatar Aug 03 '21 15:08 nkovacic

Aren't there more people having this issue. I would assume wanting the refetch to be network-only is a common use case.

flashtheman avatar Apr 13 '22 00:04 flashtheman

Set the fetchPolicy for the original useQuery hook. It forces network-only for the refetch. Worked for me.

mighty6ft5max avatar May 07 '22 14:05 mighty6ft5max

There seems to be 2 points of view in this thread:

1. People wanting to force refetch with network-only

This seems to be the default already: https://github.com/apollographql/apollo-client/blob/0daf29fc02dba3e4ba53f36a8b412abcdbe79329/src/core/ObservableQuery.ts#L361-L371

I'm I missing something?

2. People wanting to use the cache for the refetch

This seems to be a bad practice that Apollo shouldn't allow. As pointed by @micimize, the subcomponent should update automatically when the cache updates. So no need to refetch.

Can we close this issue @jpvajda?

mccraveiro avatar Dec 11 '22 20:12 mccraveiro

Hi @mccraveiro - I agree that this issue should be closed as we don't plan to change this behavior at this time. Thanks so much all for weighing in! If you're interested in pulling data from the cache at a given point, consider using readQuery / readFragment: https://www.apollographql.com/docs/react/caching/cache-interaction/

bignimbus avatar Dec 12 '22 17:12 bignimbus