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

New createMutationKeys doesn't work for mutations with arguments #55

Open
brmenchl opened this issue Mar 15, 2023 · 6 comments
Open

New createMutationKeys doesn't work for mutations with arguments #55

brmenchl opened this issue Mar 15, 2023 · 6 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed will be resolved in a major release Feature or bug will be resolved in the next major release

Comments

@brmenchl
Copy link

I first tried to follow the standard practice like for parametrized queries

export const todos = createMutationKeys('todos', {
  prop: (input: string) => ({
    mutationKey: [input],
    mutationFn: () => Promise.resolve(input),
  }),
});

...
const {mutate} = useMutation(todos.prop('hello');
// mutate has type UseMutateFunction<string, unknown, undefined, unknown>
mutate() // fails - arguments not provided (expected undefined)
mutate(undefined) // ok, returns void instead of Promise<string>

I also tried

export const todos = createMutationKeys('todos', {
  prop: {
    mutationKey: null,
    mutationFn: (input: string) => Promise.resolve(input),
  },
});

...
const {mutate} = useMutation(todos.prop);
// mutate has type UseMutateFunction<unknown, unknown, void, unknown>
mutate('hello') // fails - string is not assignable to 'void'
mutate() // ok, returns void instead of Promise<string> (but we haven't passed in the input string anywhere)

Neither gives me a workable type where I can pass in an argument. I could try to work on it myself but I'm not super familiar with this library yet.

@jetaggart
Copy link

jetaggart commented Mar 22, 2023

I'm having this issue as well. I'm using this with graphql like so:

export const checkoutMutations = createMutationKeys("checkout", {
  create: {
    mutationKey: ["create"],
    mutationFn: (items: string[]) => {
      return request(CREATE_CHECKOUT, {input: {items}})
    }
  }
})

When I try to use this like this:

const createCheckout = useMutation(checkoutMutations.create)

That works fine but when I call it later, I get an error:

const result = await createCheckout.mutateAsync(cart.items.map(item => item.url))
=> TS2345: Argument of type 'string[]' is not assignable to parameter of type 'void'.

@brmenchl
Copy link
Author

It looks like it has to do with the default type arguments in MutateFunction. Changing them to any fixes most usecases, except for when you want void as the arguments.

@lukemorales
Copy link
Owner

@jetaggart and @brmenchl thanks for bringing more context to the issue! I'll take a look on how to fix this

@gusarizona
Copy link

gusarizona commented May 23, 2023

I am not sure if this is the problem (or at least a problem), but I noticed that createMutationKeys converts the properties mutationFn to type MutateFunction instead of MutationFunction. The hook useMutation expects MutationFunction not MutateFunction (the latter is actually the type for the mutate function returned by useMutation).

@vohuynh19
Copy link

Any updates on this problem?

@lukemorales lukemorales added the help wanted Extra attention is needed label Jun 15, 2023
@Fgruntjes
Copy link

Fgruntjes commented Jun 29, 2023

Not sure about react (using svelte) but you can just pass the types with the createMutation (svelte equivalent of useMutation) function.

const institutionConnectionMutation = createMutationKeys('InstitutionConnectionMutation', {
    create: () => ({
        mutationKey: ['create'],
        mutationFn: (request: InstitutionConnectionCreateRequest) => service.create(request),
    }),
})

const mutation = createMutation<
	InstitutionConnection,
	Error,
	InstitutionConnectionCreateRequest,
	unknown
>({
	...institutionConnectionMutation.create()
});

@lukemorales lukemorales self-assigned this Feb 12, 2024
@lukemorales lukemorales added the will be resolved in a major release Feature or bug will be resolved in the next major release label Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed will be resolved in a major release Feature or bug will be resolved in the next major release
Projects
None yet
Development

No branches or pull requests

6 participants