0

I'm using apollo-link-state for locally storing errors, but I get the following error after clearing cache.

I've set the default value of errors to an empty array [] in apollo client configuration options.

However, after apolloClient.cache.reset() or apolloClient.store.reset(), it seems that I lose all default values, causing this error:

Error

Any ideas how to resolve this issue?

nomadoda
  • 4,561
  • 3
  • 30
  • 45

2 Answers2

1

From the docs:

Sometimes you may need to reset the store in your application, for example when a user logs out. If you call client.resetStore anywhere in your application, you will need to write your defaults to the store again. apollo-link-state exposes a writeDefaults function for you. To register your callback to Apollo Client, call client.onResetStore and pass in writeDefaults.

So you can do something like:

const cache = new InMemoryCache()
const link = withClientState({ cache, resolvers, defaults })

const client = new ApolloClient({
  cache,
  link,
})

client.onResetStore(stateLink.writeDefaults)
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Thanks! It didn't work at first, it seems that `writeDefaults` has some issues. Solved by adding any `Query` to client type definitions.. – nomadoda Nov 15 '18 at 15:24
0

With Apollo 2.x, you can just do below:

cache.writeData({data : defaultData });

client.onResetStore(() => {
  cache.writeData({data : defaultData });
});

Assuming that you have a default data for cache set up above this code.

Noby Fujioka
  • 1,744
  • 1
  • 12
  • 15