34

I am using apollo graphql in my react application. Say I have the following query:

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

This query lets me query a list of items using a filter. Say I used filter string A, and then used filter string B. The cache would now contain two entries: ListQuery(A) and ListQuery(B).

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

How can I accomplish this?

Andy Hansen
  • 411
  • 2
  • 5
  • 6

6 Answers6

11

In your case, you can use the apollo's method client.resetStore();

It will clear the previous cache and then load the active queries.

Nitin
  • 119
  • 1
  • 3
4

Try this:

 query ListQuery($filter: String!) {
       items(filter: $filter) {
         id
         name
       }
    },
    fetchPolicy: 'no-cache'

More here: https://www.apollographql.com/docs/react/caching/cache-interaction/#bypassing-the-cache

Giang Van
  • 41
  • 1
  • You can improve on this by still caching the main `ListQuery` query without a `filter`. – icc97 Aug 25 '23 at 21:38
3

Try evicting the particular query object from the cache...

cache.evict({ id: "ROOT_QUERY", fieldName: "listQuery" });
cache.gc();

I got this working after reading this: https://danreynolds.ca/tech/2020/05/04/Apollo-3-Client-Cache/

Danoz
  • 977
  • 13
  • 24
0

Seems like what you need is refetchQueries option/prop to be set to array of query names strings. The documentation states that:

If options.refetchQueries is an array of strings then Apollo Client will look for any queries with the same names as the provided strings and will refetch those queries with their current variables.

So as an example for using graphql HOC you could try to do:

function SomeComponent(props) {

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

  return (/* ... */)
}

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

Or with the Mutation component:

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

If this is somehow doesn't do what you need, there is also a workaround using update option.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
n1stre
  • 5,856
  • 4
  • 20
  • 41
  • 1
    Refetch sometimes doesn't clear cache, there is a bug https://github.com/apollographql/apollo-client/issues/6925#issuecomment-684104444 – Aleksandr Smyshliaev Sep 20 '22 at 06:02
  • Agree with what @AleksandrSmyshliaev said above. There are currently known issues with refetchQueries. See here for a nice explanation... https://github.com/apollographql/apollo-client/issues/5419#issuecomment-1242511457 See also here for a discussion on how to evict such a set of queries that the OP is looking to do https://github.com/apollographql/apollo-client/issues/6795 – Danoz Oct 02 '22 at 03:17
0

I was able to solve this problem by first changing a value in my schema from required to not required, which resulted in values being queried again! After that, I reverted the change and have had no problems!

Adriaan
  • 17,741
  • 7
  • 42
  • 75
-5

Read about fetch policies options.fetchPolicy

--Edit:--

But it is only as a trick if you want reset only one query. To reset all store see answer by @Nitin : Set fetchPolicy to network-only. Refresh query. Return it to option of your choice.

basil
  • 3,482
  • 2
  • 22
  • 20
  • Can you elaborate? – Robin Wieruch Feb 01 '18 at 15:56
  • This answer is not very helpful and also I think fetchPolicy and clearing the cache are two different things. – Jaakko Karhu Jun 29 '18 at 10:01
  • @JaakkoKarhu I agree. `fetchPolicy` determines how the query interacts with the cache store. For instance `fetchPolicy: 'cache-and-network'` will lookup the cache first *and* call the server. Read more about fetchPolicy here: https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy – James Dec 30 '21 at 22:11