Apollo GraphQl responds. How to clear request cache for all combinations of variables?

I am using apollo graphql in my processed application. Let's say I have the following query:

query ListQuery($filter: String!) {
   items(filter: $filter) {
     id
     name
   }
}

This query allows me to query a list of items using a filter. Let's say I used filter string A, and then used filter string B. The cache will now contain two entries: ListQuery (A) and ListQuery (B).

Now let's say I use a mutation to add a new element. How to remove all cached requests from the cache? Therefore, in this case, I want to remove both ListQuery (A) and ListQuery (B) from the cache.

How can i do this?

+19
source share
3 answers

apollo client.resetStore();

, .

+5

, , refetchQueries refetchQueries/prop . , :

options.refetchQueries - , Apollo Client , , .

graphql HOC :

function SomeComponent(props) {

  const performMutation = () => {
    props.doStuff({ refetchQueries: ["ListQuery"] })
      .then(/* ... */)
      .catch(/* ... */)
  }

  return (/* ... */)
}

export default graphql(DO_STUFF, { name: "doStuff" })(SomeComponent)

Mutation:

function SomeComponent(props) {
  return (
    <Mutation
      mutation={DO_STUFF}
      refetchQueries={mutationResult => ["ListQuery"]}>
      {(doStuff, { loading, error }) => {
        /* ... */
      }}
    </Mutation>
  );
}

- , , , update.

.

0

options.fetchPolicy

--Edit: -

But this is just a trick if you want to drop only one request. To reset all storage, see @Nitin answer: set fetchPolicy for network only . Refresh request. Return it to your choice.

-3
source

Source: https://habr.com/ru/post/1017400/


All Articles