urql: How to manually (force) invalidate cache
I might be approaching this in the wrong way but I’m in the situation where, in my app, when a user logs out, I want to invalidate the entire cache.
Coming from Apollo I’d just call client.resetStore(). Urql’s methodology of exchanges seems to work a bit differently and I can’t see any such method on the client returned from createClient.
Is this somthing I’d be able to do but manually composing the exchanges and hooking into the cache exchange or I am approaching this in completely the wrong way?
Thanks in advance
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 9
- Comments: 20 (7 by maintainers)
So if anyone else is interested in how I did this here’s an example:
Define a
makeClientfunction. Mine looks like this.Create a context that should “house” the
client. One of its props should be themakeClientfunction. You could just use thecreateClientfunction directly imported from URQL but this approach allows dependency injection so different clients can be used depending on the situation, as well as any app-specific configuration can be placed outside of this component.The context itself exposes a
resetClientfunction that simply creates a new client and updates its state with it. The URQL provider is passed it as a prop.Now, elsewhere in my app, anytime I want to flush the cache I can just get the
resetClientfunction fromuseClientand call it. URQL then refetches any queries that’re needed:Hey @slacktracer, here’s what I do. In
./setup/urql.ts:Some other file:
In
App.vue,setup()Edit: I still have to check if this also creates a new websocket connection.
I have a similar situation except for I am using an offline cache that uses indexedDB. So adding the following tip for anyone who might want to reset the client that uses offline cache. On top of the suggested answer you want to use
storage.clear().@kitten Is this still the recommended way of completely clearing/invalidating Urql’s cache? My use case is on logout. I need the cache to be completely cleared on logout. I’m fine with the introspection query being resent if necessary, which the above solution will obviously do. Thank you for your time!..and btw, Urql rocks!!
I can’t seem to make my cache clear when a user signs out (on React).
New components all mount with empty data. However, existing components do not re-render with cleared data. They still pull from the original cache.
I’ve been doing this:
I expect this to completely clear out my cache when
auth.idchanges. However, it seems like, after signing out, the cache is getting retained. The provider is at the root of my app, and I only have one, so I’m a bit confused as to how that could be happening. Does anything come to mind?In case it helps, this is my
makeClientfunction:Click to see code
I tried setting the state instead, but no luck. Even though the instance is indeed resetting, the cache remains populated with previous requests. I tried this both in dev mode with dev tools and after running
next build.I also logged
useClient’s result in my consuming components, and it is indeed a different instance:However, still no cache clearance.
I opened an issue at #2511
@davidkhierl No problem! It’s not necessarily two client instances. The second is basically overwriting the first when resetClient is called. Since it kills dev tools (or at least did last time I checked), I initially verified this was working by doing some cache queries in my code. I only reset the client on logout, so once I verified it was working, it was a set it and forget it type deal.
@davidkhierl Fyi, the initial issue I created on the DevTools repo is still open (same issue you’re experiencing): https://github.com/FormidableLabs/urql-devtools/issues/325
@Natas0007 Yep, that’s still the safest approach to make sure everything’s cleared 👍 also the introspection query is only sent in development when
@urql/devtoolsis used.Glad you resolved this! I’ll close this issue for now, since it’s technically resolved sufficiently? But maybe this would be a great addition to our docs 👍 could be just a small FAQ entry explaining that resetting the client by recreating it is the easiest way to clear all of its state