TanStackT
TanStack2y ago
18 replies
extreme-purple

Type is not assignable to type mutate function

Hello all, I get this error
Type updateDummy is not assignable to type mutation function

and I am unsure how to fix it. I followed the same pattern as my createDummy function, both of which write react query functions to make mutation calls to the database.

import { type UpdateDummyDto, type CreateDummyDto } from "@bookwave/api-client";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { DummyApiKeys } from "@/features/dummy/queries";
import { apiClient } from "@/lib/api/api-client";
import { captureAndRethrowException } from "@/lib/error/capture-and-rethrow-exception";

export const useCreateDummyMutation = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: createDummy,
    onSuccess: async () => {
      await queryClient.invalidateQueries({
        queryKey: DummyApiKeys.fetchAll(),
      });
    },
  });
};

export const useUpdateDummyMutation = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: updateDummy,
    onSuccess: async () => {
      await queryClient.invalidateQueries({
        queryKey: DummyApiKeys.fetchAll(),
      });
    },
  });
};

async function createDummy(dummy: CreateDummyDto) {
  try {
    // Artificial wait so you can see what happens on the UI while the request
    // is in flight.
    await new Promise((resolve) => {
      setTimeout(resolve, 2000);
    });

    const response = await apiClient.dummy.createOne(dummy);
    return response;
  } catch (err: unknown) {
    captureAndRethrowException(err);
  }
}

async function updateDummy(dummyId: string, dummy: UpdateDummyDto) {
  try {
    // Artificial wait so you can see what happens on the UI while the request
    // is in flight.
    await new Promise((resolve) => {
      setTimeout(resolve, 2000);
    });

    const response = await apiClient.dummy.updateOne(dummyId, dummy);
    return response;
  } catch (err: unknown) {
    captureAndRethrowException(err);
  }
}
unnamed.jpg
Was this page helpful?