Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Cache in ApolloClient exported from apollo-boost

I want to disable caching or restrict the cache to 24 hours. My ApolloClient runs exclusively on the Server side.

My environment:

  • apollo-boost 0.4.3
  • graphql 14.1.1
  • apollo-link-batch-http - 1.2.12

Right now, that's how I configure my ApolloClient.

new ApolloClient({
      ssrMode: true,
      cache: new InMemoryCache(),
      link: WithApollo.BatchLink(),
      credentials: 'same-origin',
    });

The closest thing I saw in docs is FetchOptions ... But it doesn't specify what options i can actually pass to achieve my need for disabling or restricting the cache.

like image 711
LiranC Avatar asked Dec 07 '22 11:12

LiranC


1 Answers

This is not possible with Apollo Boost. You need to migrate to migrate to using Apollo Client. This will allow you to provide a defaultOptions option to your ApolloClient constructor as shown in the docs:

const defaultOptions = {
  watchQuery: {
    fetchPolicy: 'no-cache',
  },
  query: {
    fetchPolicy: 'no-cache',
  },
}

The fetchPolicy option can actually be set on each individual query call or Query component -- by providing a defaultOptions object, you avoid having to specify no-cache as the fetch policy on each individual Query component you use. That also means if you're bent on keeping Boost, you could just do this on each of your components. However, the above is how to effectively "turn off" caching for the whole client.

like image 182
Daniel Rearden Avatar answered Dec 11 '22 11:12

Daniel Rearden