Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Params are missing in mutationKey #2100

Open
1 task
adityaa72 opened this issue Jan 13, 2025 · 3 comments
Open
1 task

Params are missing in mutationKey #2100

adityaa72 opened this issue Jan 13, 2025 · 3 comments
Assignees
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-react-query Relevant to openapi-react-query

Comments

@adityaa72
Copy link

openapi-react-query version

0.2.9

Description

MutationKey don't include params as can be seen in this code

useMutation(
        {
          mutationKey: [method, path],
          mutationFn: async (init) => {
            const mth = method.toUpperCase() as Uppercase<typeof method>;
            const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
            const { data, error } = await fn(path, init as InitWithUnknowns<typeof init>);
            if (error) {
              throw error;
            }

            return data as Exclude<typeof data, undefined>;
          },
          ...options,
        },
        queryClient,
      )

Use case

I wanna check if a mutation with a specific id is mutating or not.
Here id should be used for creating mutation key

Also there is a request to add createQueryKey function in your package

import { createQueryKey } from "@/client/utils";
import { useIsMutating } from "@tanstack/react-query";

export const usePayoutWalletState = (id: string) => {
  const isMutating = useIsMutating({
    mutationKey: createQueryKey("put", "/payout-wallets/{id}/set-default"),
  });
  return {
    isMutating,
  };
};

Reproduction

This issue is not related to any browser or specific conditions.

Expected result

I expected that mutationKey should include params same as used in queryOptions

const queryOptions: QueryOptionsFunction<Paths, Media> = (method, path, ...[init, options]) => ({
    queryKey: [method, path, init as InitWithUnknowns<typeof init>] as const,
    queryFn,
    ...options,
  });

Extra

@adityaa72 adityaa72 added bug Something isn't working openapi-react-query Relevant to openapi-react-query labels Jan 13, 2025
@kerwanp
Copy link
Contributor

kerwanp commented Jan 23, 2025

You are right, we should be providing parameters to the mutationKey, the same way we do with queryKey. Happy to review a PR for that!

@kerwanp kerwanp added the good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project label Jan 23, 2025
@musjj
Copy link

musjj commented Jan 23, 2025

It seems that init has to be passed late unlike useQuery, so I don't think that this is possible.

Furthermore, it looks like that the role of queryKey and mutationKey is very different, so I don't think it's the right tool for checking specific mutations.

@kerwanp
Copy link
Contributor

kerwanp commented Jan 23, 2025

A feature that is not implemented in openapi-react-query is the ability to provide parameters in useMutation that become optional when using mutate. In this case, it would be possible to use those initial parameters as a mutation key.

const { mutate } = $query.useMutation('GET', '/api/{authorId}/posts', { params: { path: { authorId: 2 } } }) 

// authorId is now optional
mutate({
  params: { query: { page: 4 } }
})

As I have never used mutation keys before, I am not really aware of the different use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-react-query Relevant to openapi-react-query
Projects
None yet
Development

No branches or pull requests

4 participants
@kerwanp @musjj @adityaa72 and others