I'm using apollo-client, apollo-link and react-apollo, I want to fully disable cache, but don't know how to do it.
I read the source of apollo-cache-inmemory
, it has a config
argument in its constructor, but I can't build a dummy storeFactory
to make it works.
To disable normalization for a type, define a TypePolicy for the type (as shown in Customizing cache IDs) and set the policy's keyFields field to false . Objects that are not normalized are instead embedded within their parent object in the cache.
Overview. Apollo Client stores the results of your GraphQL queries in a local, normalized, in-memory cache. This enables Apollo Client to respond almost immediately to queries for already-cached data, without even sending a network request. The Apollo Client cache is highly configurable.
In your case, you can use the apollo's method client. resetStore(); It will clear the previous cache and then load the active queries.
The opposite of no-cache, this fetch policy avoids making any network requests. If the data you are querying is not available in the cache, it will throw an error.
You can set defaultOptions
to your client like this:
const defaultOptions: DefaultOptions = { watchQuery: { fetchPolicy: 'no-cache', errorPolicy: 'ignore', }, query: { fetchPolicy: 'no-cache', errorPolicy: 'all', }, } const client = new ApolloClient({ link: concat(authMiddleware, httpLink), cache: new InMemoryCache(), defaultOptions: defaultOptions, });
fetchPolicy
as no-cache
avoids using the cache.
See https://www.apollographql.com/docs/react/api/core/ApolloClient/#defaultoptions
Actually, setting fetchPolicy
to network-only
still saves the response to the cache for later use, bypassing the reading and forcing a network request.
If you really want to disable the cache, read and write, use no-cache
. Which is "similar to network-only, except the query's result is not stored in the cache."
Take a look at the official docs: https://www.apollographql.com/docs/react/data/queries/#configuring-fetch-logic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With