-
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
cache.modify INVALIDATE doesn't result in query refetching data #7060
Comments
@vedrani You're right, To make the most of export default function Todo({ todo }) {
const [updateTodo] = useMutation(UPDATE_TODO, {
update(cache, { data: { updateTodo } }) {
cache.modify({
id: cache.identify(updateTodo),
fields(fieldValue, details) {
return details.INVALIDATE;
}
});
},
// This callback will be called with each ObservableQuery that was
// affected by the mutation update function, so you can decide how
// to refetch the query. The default behavior is the same as calling
// observableQuery.reobserve(), but calling refetch instead will force
// a network request.
onQueryUpdated(observableQuery) {
return observableQuery.refetch();
},
}); I'll let you know when that's ready for testing (or you can watch #7015 for upcoming betas). Thanks for trying out the |
@benjamn I'm also interested are we going to have utility that will allow us to just watch dirty status in cache for some query? Or query callback will be enough? What is interesting for me is case of expensive or slow query. Will I have opportunity just to show for example Reload button instead of refetching query immediately? Thanks! |
@benjamn what about queries that are not currently active? I mean, I have 2 pages, in page 1 I have a list of items, in page 2 I have a function where I can query some data or do something and I need to invalidate data from page 1. Then when I navigate to page 1, I want to show existing data but since cache data is invalidated, I want to refetch data form the server. The fetch policy in page 1 query is cache first The way I see INVALIDATE or how I would like to use it in this case is whenever I go to page 1, if data is valid and fetchPolicy is |
@benjamn I've tried invalidating a field using
similar situation when i try substituing the following
I've also tried the following as part of my apollo client config as mentioned here without any luck there either
Probably worth adding that this field has a typePolicy for pagination associated with it in case that has some bearing on implementation bugs. I have to say I've been continually gotcha'd by the over-engineering in Apollo client. The semantics of INVALIDATE should probably be as simple as possible. If I invalidate cache data, no matter what the fetchPolicy is, any subsequent requests to that field (or fields) in the cache should miss and issue a network request. |
Definitely agreed. I spent an unfortunate amount of time today misunderstanding INVALIDATE semantics. @benjamn If INVALIDATE is not meant to trigger a refetch, what is the correct way to indicate to Apollo client that a specific field is invalid and should trigger a refetch? |
I have successfully used the DELETE sentinel instead of the INVALIDATE sentinel to force a refetch with a cache-first fetch policy. Not sure if this was the new addition mentioned by @benjamn but it appears to work correctly. |
PR in progress for the |
@benjamn I don't understand how |
I'm having trouble here deciding between |
@ab-pm DELETE means that until data comes back, the data is empty, isn't it? and that is not a good user experience.. |
@omerman Oops you're right. I hadn't noticed the flicker at first because the backend responded so quickly. I had expected that when a query is refetched, the Edit: there's |
I agree with everyone else here, when I use INVALIDATE I expect Apollo to re-fetch automatically as if the cache was empty. I expect If About the |
I also agree with all the comments here. I would like a way to do BOTH of these things upon successful completion of a mutation:
The first bullet can be solved by |
So to sum up
|
Just to add to @dylanwulf 's point and summarize a bit what I think about this:
EDIT: about the last point, I hadn't noticed the global |
Intended outcome:
My motivation is to invalidate data in cache, so that queries can refetch invalidated data.
(For that reason I just return
_id
from mutation. I know I can return data with mutation, but this scenario has implication on invalidating data on mutations with side effects.)For that purpose I'm using
cache.modify
and INVALIDATE flag to invalidate data in cache based on docs.My expectation is that invalidation will result in query refetching data from server because data is not valid any more.
I also tried
evict
and it works, but my impression is thatinvalidate
mechanism should allow us to refetch data based on dirty status. Did I miss something in code or did I misunderstood it? Thanks!Codesandbox
Actual outcome:
Query that fetches all todos is reobserved but it doesn't refetch data.
I tried to debug it and it seems cache diff is non existent. After cache.modify invalidation, query is reobserved but data is resolved from cache instead refetched.
What I found is that INVALIDATE is translated into
dirty
status here, but Query when calculating diff doesn't see any difference hereHow to reproduce the issue:
Open Codesandbox and try to change status of todo item.
It doesn't change.
Versions
System:
OS: macOS 10.15.5
Binaries:
Node: 14.2.0 - ~/.nvm/versions/node/v14.2.0/bin/node
Yarn: 1.22.5 - ~/.yvm/versions/v1.22.5/bin/yarn
npm: 6.14.4 - ~/.nvm/versions/node/v14.2.0/bin/npm
Browsers:
Chrome: 85.0.4183.121
Firefox: 75.0
Safari: 13.1.1
npmPackages:
@apollo/client: 3.2.0 => 3.2.0
The text was updated successfully, but these errors were encountered: