How to make optimistic/pessimistic cache update (probably with updateQueryData
), but without knowing the arguments of previous queries?
updateQueryData(
"getItems",
HERE_ARE_THE_ARGUMENTS_I_DONT_HAVE,
(data) => {
...
}
)
getPosts
query with args search: number
updatePosts
mutationExample actions:
getPosts
with search = ""
is cached.abc
.getPosts
with search = "abc"
is cached.But I need universal solution. I don't know how many different cached entries will be there. And also my case is more complex, because I have other args than "search" to worry about (pagination).
updateQueryData
for each of them, but I cannot find simple way to do that.getState()
within onQueryStarted
and retrieving query parameters from there (in order to do above), but it's not elegant wayOkay, I found solution here Is it possible to optimistically update all caches of a endpoint?
Using
api.util.selectInvalidatedBy(getState(), { type, id })
gives you in return list of cached queries with endpointNames
and queryArgs
. Then you can easily mutate these caches by updateQueryData
.
There's also new problem created here, which is situation where modifying some particular properties may change response from API in unpredictable way. For example in a list you may sort by status, which you can modify. Then changing the cache would be bad idea and instead of that I've created new tag which is invalidated with each update { type: "getItems", id: "status" }
and for getItems
it would be like { type: "getItems", id: sortBy }