question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
hyochancommented, Apr 15, 2020

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.

11reactions
tafelnlcommented, May 5, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refetching queries in Apollo Client - Apollo GraphQL Docs
In theory, you could refetch every active query after a client-side update, but you can save time and network bandwidth by refetching queries...
Read more >
Queries - Apollo GraphQL Docs
Refetching enables you to refresh query results in response to a particular user action, as opposed to using a fixed interval. Let's add...
Read more >
When To Use Refetch Queries in Apollo Client
The disadvantage is that we're fetching the entire list of data again when we might not need to. Cache Normalization and Update Functions....
Read more >
Advanced topics on caching in Apollo Client
In cases like this, use the no-cache fetch policy: JavaScript ... This can be useful if you want to force your UI to...
Read more >
Advanced HTTP networking - Apollo GraphQL Docs
You can also use it to replace Apollo Client's networking layer with something ... A string representing the credentials policy to use for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found