Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refetch returned cached data #3243

Closed
dennislaupman opened this issue Mar 30, 2018 · 10 comments
Closed

Refetch returned cached data #3243

dennislaupman opened this issue Mar 30, 2018 · 10 comments

Comments

@dennislaupman
Copy link

dennislaupman commented Mar 30, 2018

Hi,

const getBookmarks = graphql(BookmarkedViewQuery, {
options: ( props ) => {
return {
fetchPolicy: 'network-only', // skip the cache
}
} ,
});

After a mutation it seems like:
componentWillReceiveProps(nextProps) {
// console.log("refresh");
this.props.data.refetch();
}
will return cached data

@evans
Copy link
Contributor

evans commented Apr 6, 2018

Let's see if apollographql/react-apollo#1901 fixes the issue when it is released. Could you create a small reproduction as well?

@colevels
Copy link

colevels commented Apr 9, 2018

I have the same problem. Sometimes props.refetch return cached data to a child component.

@hwillson
Copy link
Member

hwillson commented Aug 5, 2018

This should no longer be an issue using a recent version of Apollo Client / React Apollo. Closing for now, but if anyone is still encountering this, please let us know (ideally with a small runnable reproduction). Thanks!

@hwillson hwillson closed this as completed Aug 5, 2018
@agu-z
Copy link

agu-z commented Aug 30, 2018

@hwillson This still happens to me in the latest version. It's pretty particular because it doesn't always happen but under certain conditions. It's pretty hard to isolate, but think it might be related to refetching two or more components at the same time.

@agu-z
Copy link

agu-z commented Aug 30, 2018

I wrapped the call in a timeout, and the updated data was passed to the component:

setTimeout(() => {
  this.props.refetch();
}, 0);

In my Network tab, this query is no longer being batched with the rest. I assume because it's deferred. I disabled batching by just using apollo-link-http (instead of apollo-link-batch-http), but it kept happening. So I don't think this is directly related to batching, but I'm not sure how Apollo works internally.

Hope that helps.

@justinmchase
Copy link

justinmchase commented Aug 24, 2019

I can see the call being made to the server but logging in the client shows the same data as the previous call. Too many layers to make a small repro yet but it does seem to be happening still.

It could be because I have the same input variables as the previous call? Its a time based event though so it should actually refetch and return the new result.

@anmolchandra96
Copy link

anmolchandra96 commented Sep 6, 2019

the refetch function called follows the fecthPolicy of its query function. If the fetchPolicy is set to cache-and-network refetch gets the data from the cache and returns it while simultaneously making a network call and subsequently sets the new data in the cache.

Is there a way for me to keep the fetchPolicy in the query as cache-and-network but make sure that refetch returns the data from the network call instead of from cache

@Pajn
Copy link
Contributor

Pajn commented Nov 13, 2019

This is still an issue in fully updated versions, can we reopen this issue?

@Natteke
Copy link

Natteke commented Mar 24, 2020

I think this is really important feature, usually you don't want to use "network-only" to prevent irrational network calls, but in 90% cases if user want to refetch a data, you want to show him a loader/spinner which will be hidden once refetch was complete, not instantly after it was shown.

@Nate-Wilkins
Copy link

I dug into this a lot more and determined that this line of code was the culprit.

if (shouldWriteResult(result, options.errorPolicy)) {

Its not really a bad assumption to make that if the result of the refetch is the same as the last query call result then don't update the cache. In my case this came up because I had a test that was mocking out an endpoint of resource types (ie a list of data types) and a patch call to update one of those data types. The problem arose when refetching but that refetch didn't provide the updated list of data types and instead had the same list with no changes - hence why the write was skipped and the cached data was returned.

I really hope that helps someone else because this was a pain to track down.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants