LiranC
LiranC

Reputation: 2480

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:

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.

Upvotes: 4

Views: 5611

Answers (2)

Raymond Liao
Raymond Liao

Reputation: 1795

Maybe someone want to know how to disable caching of apollo-boost ApolloClient exactly, so let's talk about it.

@Daniel said is truth, we cannot disable the caching when we do new ApolloClient of apollo-boost directly, but we can set fetchPolicy when we send out the request. The code as below:

  // create client first
  import ApolloClient from "apollo-boost";
  const client = new ApolloClient({ uri: GRAPHQL_URL })

  // Set the fetchPolicy when we send request
  import { gql } from 'apollo-boost';
  client.query({
    query: gql`
      query someInfo($id: ID!) {
        info(id: $id) {
          id
          name
        }
      }`,
    variables:{id: '123'},
    fetchPolicy: 'no-cache'
  })

The valid value for fetchPolicy you can find from there.

Upvotes: 7

Daniel Rearden
Daniel Rearden

Reputation: 84687

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.

Upvotes: 7

Related Questions