It's not possible to reset store without query refetching
See original GitHub issueI am using following middleware to handle authorization:
networkInterface.use([{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {};
}
const accessToken = store.state.session.accessToken;
if (accessToken) {
req.options.headers['authorization'] = `Bearer ${accessToken}`;
}
next();
}
}]);
In my component (for authenticated users) I use following query:
apollo: {
projects: {
query: gql`
query projectList {
projects {
id
name
created_at
}
}
`,
fetchPolicy: 'cache-and-network',
},
},
When user logs out, I clear accessToken, call apollo.resetStore()
and redirect to /login
page.
The problem is that after calling apollo.resetStore()
apollo automatically refetches projectList query, however it receives 401 error since accessToken had been cleared. I tried to use $skipAll
option like this:
apollo: {
projects: {
query: gql`
query projectList {
projects {
id
name
created_at
}
}
`,
fetchPolicy: 'cache-and-network',
},
$skipAll () {
console.log('SKIP', !this.isLoggedIn);
return !this.isLoggedIn;
},
},
But it is not called at all when apollo automatically refetches queries after apollo.resetStore()
. Is there any other way to clearStore without refetching queries?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:11 (1 by maintainers)
Top Results From Across the Web
resetStore: Store reset while query was in flight. #2919 - GitHub
Intended outcome: resetStore without errors being thrown. ... I understand that calling client.cache.reset() is not nice, thats why I added:.
Read more >clearStore() vs resetStore() - Help - Apollo GraphQL
It mentions resetStore() will “cause the store to be cleared and all active queries to be refetched.” What exactly is an active query,...
Read more >Reset store after logout with Apollo client - Stack Overflow
Remove all data from the store. Unlike resetStore, clearStore will not refetch any active queries. Share.
Read more >React Query Refetching Stale Data - React Tutorial 88
FREE Courses (100+ hours) - https://calcur.tech/all-in-ones Python Course - https://calcur.tech/python-courses✓ Data Structures ...
Read more >Redux Essentials, Part 8: RTK Query Advanced Patterns
When we opened the <PostsList> component again, RTK Query saw that it did not have the data in cache and refetched it. There...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@alekbarszczewski
you can try this: client.cache.reset()
For others with multiple clients, you can run this from inside a component: