TanStackT
TanStack3y ago
2 replies
awake-maroon

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();
    },
  });

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;
        }
      );
    },
  });
Was this page helpful?