Description
Hi,
I noticed that the docs are being changed to better convey how to perform manual cache updated here 1525, but I feel that the current implementation can be a bit misleading and lead devs to unwittingly create bugs.
I think that most people think of manual cache updates as changing a cache entry without triggering a new query request (ie invalidating tags), and for the most part this is possible.
However, if the first query request errored, or the entry hasn't been created, or the dev calls resetApiState
, then truly manual cache updates will all fail.
Here's a use case. Let's say that the dev is using rtkq to manage the current user session (maybe user sessions isn't the most common use case, but I'm sure other cases could have similar issues):
1 - Presumably, on app load there'll be a getCurrentSession
request.
2 - The user logs in and the request returns the session data. The dev doesn't want to invalidate the getCurrentSession tag and only wants to replace the cache entry with the data from the login request.
3 - Now the problem is that if the first getCurrentUser
request for some reason failed (the cache has no data, only the error property) or if on logout the dev calls resetApiState
, manual cache updates won't work and login will be broken.
This in some sense also breaks optimistic updates, technically the data will show up, but it only after tag invalidation and its respective request, which could be a confusing behavior.
I'm aware that the docs do indicate 'if a cache entry exists', but it might not be obvious that this applies to a query that errored on first (only) request and after resetApiState
calls. It's very easy to assume that manual cache updates would always work no matter what.
- So basically I'm wondering if updating a cache entry that has errored or doesn't exist is something that might be added as a feature (if I recall correctly this is supported by react-query).
- Alternatively, perhaps the docs should indicate more clearly that manual cache updates won't work quite as expected if the cache entry errored on first request or doesn't exist (either due to never having been called or due to a
resetApiState
call) and that you should always useinvalidatingTags
whatever strategy you use to update cache entries.
Anyway, thanks for you work.