-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Comments
Let's see if apollographql/react-apollo#1901 fixes the issue when it is released. Could you create a small reproduction as well? |
I have the same problem. Sometimes props.refetch return cached data to a child component. |
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 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. |
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 Hope that helps. |
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. |
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 |
This is still an issue in fully updated versions, can we reopen this issue? |
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. |
I dug into this a lot more and determined that this line of code was the culprit. apollo-client/src/core/QueryInfo.ts Line 308 in 7a022e6
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. |
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
The text was updated successfully, but these errors were encountered: