How to force update data cache in react-apollo?

Pranesh Ravi picture Pranesh Ravi · May 5, 2017 · Viewed 9.3k times · Source

How to refetch fresh data when you revisit a page whose data is powered by react-apollo?

Say, I visit a listing page for the first time. apollo will fetch the query and caches it by default. So, when you visit the same page again during the session, it will populate the data from its cache store. How to force apollo to refetch data every time when the component mounts?

Answer

Pranesh Ravi picture Pranesh Ravi · May 5, 2017

You can use apollo's fetchPolicy. Based on this, it will decide to execute the query or not again.

Example:

const graphQLOptions = {
  name: 'g_schemas',
  options: (props) => {
    return {
      variables: {
        name: props.name,
      },
      fetchPolicy: 'cache-and-network',
    }
  },
}

Hope it helps.