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.

It's not possible to reset store without query refetching

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

21reactions
Risbotcommented, Mar 21, 2018

@alekbarszczewski

you can try this: client.cache.reset()

6reactions
Adverblycommented, Mar 27, 2018

For others with multiple clients, you can run this from inside a component:

Object.values(this.$apollo.provider.clients)
    .forEach(client => client.cache.reset())
Read more comments on GitHub >

github_iconTop 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 >

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