Apollo Client 3 evict query result - doesn't work?
Asked Answered
K

1

6

Using Apollo Client 3 and eed to remove all the results of a query regardless of the arguments. Tried like this;

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

        cache.gc();

However nothing removed from cache, __APOLLO_CLIENT__.cache.data still has all the result in the cache. I can remove a single Country object on the other hand.

You can see it here sandbox

Klimt answered 31/10, 2020 at 10:11 Comment(6)
reseting store is for thatWellgroomed
Couldn't find any sample !...Klimt
I want to remove only a specific query's result, not to clear whole store.Klimt
ok, I explored this problem a bit ... cache entry is deleted (console.log("cache", cache.data.data);), - gc() doesn't matter ... but problem is with hooks, view isn't rerendered when 'data' is cleared ... IMHO it's an apollo issue as change is propagated ( this.broadcastWatches(); called), 'data' should be set to undefined again .... workaround? writeQuery with ampty data?Wellgroomed
thanks @xadm. You're right, it's about useLazyQuery and cache.evict({ id: "ROOT_QUERY", fieldName: "countries" }); After this cache.evict, apollo client somehow can not reconcile query and the cache, so it queries again. Do you think that we can use this same useLazyQuery hook in out page with different fetch policies to overcome this?Klimt
not refetched (check network tab), just not catched (by hook internal observable) cache entry change ... probably you can force omponent rerendering by refetch (from hook) called with cache-only policyWellgroomed
K
7

Simple adding broadcast: false parameter solved the problem;

               cache.evict({
                  id: 'ROOT_QUERY',
                  fieldName: 'countries',
                  broadcast: false,
                });
                cache.gc();
Klimt answered 4/11, 2020 at 8:21 Comment(1)
broadcast : false did the trick, why is it not there in the docs or I am not able to find it :/Bracket

© 2022 - 2024 — McMap. All rights reserved.