Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable cache in apollo-link or apollo-client?

Tags:

react-apollo

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.

like image 278
Jacky Lee Avatar asked Dec 19 '17 02:12

Jacky Lee


People also ask

How do I disable Apollo Client cache?

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.

Does Apollo Client 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.

How do I clear my cache in GraphQL?

In your case, you can use the apollo's method client. resetStore(); It will clear the previous cache and then load the active queries.

What is Fetchpolicy no cache?

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.


2 Answers

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

like image 173
Irvin Chan Avatar answered Sep 22 '22 09:09

Irvin Chan


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

like image 22
duske Avatar answered Sep 19 '22 09:09

duske