Do optimistic updates work with resource creation and infinite queries? #2243
-
I've got optimistic updates working well for updating or deleting resources within an infinite list. I also have optimistic updates working for updating a single item. What I can't get to work, are optimistic responses for new resources which are added to infinite lists. I've confirmed the data is correct and I've dug through some of the React Query source code but haven't found my problem yet. Any ideas? Here's my onMutate: async mutationArgs => {
await queryClient.cancelQueries(queryKey);
const newNotification: ManualNotificationsItemFragment = {
id: '-1',
email_count: 0,
user_count: 0,
created_at: 0,
segments: [],
send_at: null,
...mutationArgs.data,
};
const baseState = queryClient.getQueryData<QueryData>(queryKey);
const nextState: QueryData['pages'] = baseState.pages.map((page, index) => {
if (index !== 0) {
return page;
}
return {
getManualNotificationItems: {
...page.getManualNotificationItems,
count: page.getManualNotificationItems.count + 1,
items: [newNotification].concat(page.getManualNotificationItems.items),
},
};
});
if (baseState) {
queryClient.setQueryData<QueryData>(queryKey, {
...baseState,
pages: nextState,
});
}
return { baseState };
}, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found the problem. The queries were using |
Beta Was this translation helpful? Give feedback.
I found the problem. The queries were using
notifyOnChangeProps
and it was set toisFetching
from early testing. As a result, the queries weren't notified of changes to the underlying data. I'm in the middle of a huge migration from Apollo and appreciate the flexibility of this library. Thanks for all the great work.