React Query useMutation - “没有重载与此调用匹配”

Jam*_*ill 5 typescript graphql react-query

我在打字稿项目中使用react-query,并且在尝试将 useMutation() 挂钩与 graphql 查询一起使用时遇到难以解释的类型错误。

示例代码:

useMutation(
    async (
      parameter1: string,
      parameter2: string
    ) => {
      const response = await sdk.myMutation({
        parameter1: parameter1,
        parameter2: parameter2,
      });
      return response;
    },
    {
      onSettled: () => queryClient.invalidateQueries([CACHE_KEY]),
    }
);
Run Code Online (Sandbox Code Playgroud)

类型错误:

  No overload matches this call.
  Overload 1 of 4, '(mutationFn: MutationFunction<{ __typename: "IdResponse"; id: string; }, string>, options?: Omit<UseMutationOptions<{ __typename: "IdResponse"; id: string; }, unknown, string, unknown>, "mutationFn"> | undefined): UseMutationResult<...>', gave the following error.
    Argument of type '(parameter1: string, parameter2: string) => Promise<{ __typename: "IdResponse"; id: string; }>' is not assignable to parameter of type 'MutationFunction<{ __typename: "IdResponse"; id: string; }, string>'.
  Overload 2 of 4, '(mutationKey: MutationKey, options?: Omit<UseMutationOptions<unknown, unknown, void, unknown>, "mutationKey"> | undefined): UseMutationResult<unknown, unknown, void, unknown>', gave the following error.
    Argument of type '(parameter1: string, parameter2: string) => Promise<{ __typename: "IdResponse"; id: string; }>' is not assignable to parameter of type 'MutationKey'.
  Overload 3 of 4, '(mutationKey: MutationKey, mutationFn?: MutationFunction<unknown, void> | undefined, options?: Omit<UseMutationOptions<unknown, unknown, void, unknown>, "mutationFn" | "mutationKey"> | undefined): UseMutationResult<...>', gave the following error.
    Argument of type '(parameter1: string, parameter2: string) => Promise<{ __typename: "IdResponse"; id: string; }>' is not assignable to parameter of type 'MutationKey'.
Run Code Online (Sandbox Code Playgroud)

Jam*_*ill 8

虽然对我来说并不明显,但问题是我尝试将多个参数传递给 useMutation 中使用的函数。

MutationFunction类型仅接受单个参数(令人困惑地称为variables)。 https://tanstack.com/query/v4/docs/reference/useMutation

为了解决这个问题,我将包含参数的对象传递给函数,如下所示:

useMutation(
    async ({
      parameter1,
      parameter2
    }:{
      parameter1: string,
      parameter2: string
      }
    ) => {
      const response = await sdk.myMutation({
        parameter1: parameter1,
        parameter2: parameter2,
      });
      return response;
    },
    {
      onSettled: () => queryClient.invalidateQueries([CACHE_KEY]),
    }
);
Run Code Online (Sandbox Code Playgroud)

一个更简洁的想法可能是创建一个类型来保存这些参数之类的东西。

type myMutationParams ={
  parameter1: string;
  parameter2: string;
}
Run Code Online (Sandbox Code Playgroud)

那么 useMutation 钩子看起来像这样:

useMutation(
    async ({
      parameter1,
      parameter2
    }: myMutationParams
    ) => {
      const response = await sdk.myMutation({
        parameter1: parameter1,
        parameter2: parameter2,
      });
      return response;
    },
    {
      onSettled: () => queryClient.invalidateQueries([CACHE_KEY]),
    }
);
Run Code Online (Sandbox Code Playgroud)