Search code examples
react-apollo

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


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.


Solution

  • 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