T
TanStack3y ago
ambitious-aqua

Weird behaviour with setQueriesData

I have a mutation that I want to use the setQueriesData feature. The behaviour I'm seeing, is that the query gets invalidated despite the fact that I'm setQueriesData is called, and not invalidateQueries. I've tried creating a reproduction but couldn't. Is there any reason why using setQueriesData would also invalidate the query? The query involved is as follows:
const listResourcesQuery = createQuery({
queryKey: () => [
resourcesKeys.listResources,
projectState.projectId,
resourceReportFilter(),
...
createReportPageSettings(),
],
queryFn: () => {
const body: ListResourcesRequest = createListResourcesRequest({
guid: defaultReportQuery.data?.guid!,
...
currentPage: paginationState.currentPage,
});
return resourcesApi.listResources(body);
},
get enabled() {
return !!resourceReportFilter();
},
});
const listResourcesQuery = createQuery({
queryKey: () => [
resourcesKeys.listResources,
projectState.projectId,
resourceReportFilter(),
...
createReportPageSettings(),
],
queryFn: () => {
const body: ListResourcesRequest = createListResourcesRequest({
guid: defaultReportQuery.data?.guid!,
...
currentPage: paginationState.currentPage,
});
return resourcesApi.listResources(body);
},
get enabled() {
return !!resourceReportFilter();
},
});
And the mutation function is
const updateResourceMutation = createMutation({
mutationFn: (body: UpdateResourceRequest) =>
resourcesApi.updateResource(body),
onSuccess: (r) => {
queryClient.setQueriesData(
[resourcesKeys.listResources, projectState.projectId],
(oldData: QuestResource[] | undefined) => {
if (oldData === undefined) {
return undefined;
}
const idx = oldData.findIndex((d) => d.id === r.id);
const newData = Object.assign([], oldData, {
[idx]: r,
}) as QuestResource[];
return newData;
}
);
},
});
const updateResourceMutation = createMutation({
mutationFn: (body: UpdateResourceRequest) =>
resourcesApi.updateResource(body),
onSuccess: (r) => {
queryClient.setQueriesData(
[resourcesKeys.listResources, projectState.projectId],
(oldData: QuestResource[] | undefined) => {
if (oldData === undefined) {
return undefined;
}
const idx = oldData.findIndex((d) => d.id === r.id);
const newData = Object.assign([], oldData, {
[idx]: r,
}) as QuestResource[];
return newData;
}
);
},
});
2 Replies
foreign-sapphire
foreign-sapphire3y ago
not that I know of
ambitious-aqua
ambitious-aquaOP3y ago
Thank you for getting back to me. It turns out it's caused by an idiosyncrasy of the backend API - the pagination data is returned in the first member of the list response, but it's missing in the update response, so when updating the list with the update response, the pagination data becomes undefined, causing invalidation as it's part of the queryKey. Should I delete this post, I don't think it adds much value to the Solid Query knowledge base?

Did you find this page helpful?