Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force update data cache in react-apollo?

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?

like image 472
Pranesh Ravi Avatar asked May 05 '17 03:05

Pranesh Ravi


People also ask

How do you update cache after mutation Apollo?

Updating a single existing entity If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns. To do so, the mutation must return the id of the modified entity, along with the values of the fields that were modified.

How do I reset my Apollo cache?

Resetting the cache Sometimes, you might want to reset the cache entirely, such as when a user logs out. To accomplish this, call client. resetStore . This method is asynchronous, because it also refetches any of your active queries.


2 Answers

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.

like image 73
Pranesh Ravi Avatar answered Sep 18 '22 08:09

Pranesh Ravi


Adding to Pranesh's answer: the fetchPolicy you're looking for is network-only.

like image 29
Ricardo Portugal Avatar answered Sep 20 '22 08:09

Ricardo Portugal