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

resetStore doesn't clear the cache if it was populated with cache's .restore method initially and no queries are run on the page #3539

Closed
d-dmytro opened this issue Jun 3, 2018 · 2 comments
Assignees

Comments

@d-dmytro
Copy link

d-dmytro commented Jun 3, 2018

Intended outcome:
I used the client.resetStore() method to clear cache after logging out a user. I populate the cache with the data from the server initially (on page load) using the cache's .restore method, like this:

new ApolloClient({
    connectToDevTools: true,
    ssrMode: false,
    link: createUploadLink({
        uri: `${apiHost}/graphql`,
        credentials: "include",
        headers: { cookie: options.cookie }
    }),
    cache: new InMemoryCache().restore(initialState || {})
});

No queries or mutations happen on the page initially. When the user clicks logout, I expect apollo-client to clear the cache that I populated initially.

Actual outcome:

When the user logs out, the client.resetStore() method does not clear the data that was populated with the InMemoryCache's restore method. The cache is not cleared as the QueryManager is not initialized at the moment I call the resetStore (or the user logs out), it is initialized only when some query or mutation occurs. This is where apollo-client checks whether the QueryManager exists, and if not it skips clearing the cache:
https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/ApolloClient.ts#L402

How to reproduce the issue:
I described how to reproduce the issue in the text above. Basically, you need to init the ApolloClient with cache populated using the .restore() method. The app shouldn't fire any query or mutation on initialization, which causes this.queryManager to not be initialized.

Versions
Binaries:
Node: 8.9.4 - /usr/local/bin/node
Yarn: 1.3.2 - /usr/local/bin/yarn
npm: 5.6.0 - /usr/local/bin/npm
npmPackages:
apollo-cache-inmemory: ^1.2.1 => 1.2.1
apollo-client: ^2.3.1 => 2.3.1
apollo-upload-client: ^8.0.0 => 8.0.0
react-apollo: ^2.1.4 => 2.1.4

@d-dmytro d-dmytro changed the title resetStore doesn't clear the cache if it was populated with cache's .restore method resetStore doesn't clear the cache if it was populated with cache's .restore method initially and no queries are run on the page Jun 3, 2018
@hwillson hwillson self-assigned this Sep 5, 2018
hwillson added a commit that referenced this issue Sep 5, 2018
A new `clearStore` method has been added, that will remove all
data from the store. Unlike `resetStore`, it will not refetch
active queries after removing store data.

Fixes #2411.
Fixes #2774.
Fixes #3539.
Fixes #3813.
hwillson added a commit that referenced this issue Sep 5, 2018
* Add `clearStore` method

A new `clearStore` method has been added, that will remove all
data from the store. Unlike `resetStore`, it will not refetch
active queries after removing store data.

Fixes #2411.
Fixes #2774.
Fixes #3539.
Fixes #3813.

* Changelog update

* Add `clearStore` to API docs

* Add `clearStore` reference to authentication docs

* Add `clearStore` reference to caching docs
@hwillson
Copy link
Member

hwillson commented Sep 5, 2018

#3885 adds a new client.clearStore() method to the public API, that will clear the store without refetching active queries. Thanks!

@d-dmytro
Copy link
Author

Thank you :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 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

2 participants