Repository navigation
How would one go about optimistically updating cache based on a paginated endpoint? #2968
Unanswered
LoyalPotato
asked this question in
Q&A
Replies: 1 comment
|
Have the same question. It doesn't make much sense to me to pass pagination params to mutations so I could match the request correctly. Must be a better way. Optimistic update however makes sense only for the active queries (i.e. that data currently visible on the screen). The inactive ones can catch up once update is finished and then tag invalidates and request re-fires. UPD: there are a few fns that can be used to get those query args - selectInvalidatedBy or selectCachedArgsForQuery so looks like this (here I'm updating an active flag for a single user in the list) async onQueryStarted(
{ node, endUser, active },
{ dispatch, queryFulfilled, getState }
) {
const queries = endUsersApiSlice.util.selectInvalidatedBy(getState(), [
getEndUsersQueryTag(node.id),
]);
const patchResults = queries.map((query) =>
dispatch(
endUsersApiSlice.util.updateQueryData(
'getEndUsers',
query.originalArgs,
(endUsersCache) => {
const user = endUsersCache?.items?.find(
(u) => u.userId === endUser.userId
);
if (user) {
user.active = active;
}
}
)
)
);
try {
await queryFulfilled;
} catch {
patchResults.forEach((patchResult) => patchResult.undo());
}
},complicated tho |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hello,
Say you have an API that implements the same pagination structure as the one mentioned in the docs.
How would you perform an optimistic update if you don't know in which page the item you're optimistically updating is inserted on.
Since the queries cache based on the params would this even be possible?
For example:
I'm updating the item
{id: 105, name: "Foo"}which is returned when I make a GET request to the API with page 5.How would I update the data for that page request.
Thanks
All reactions