query-key-factory icon indicating copy to clipboard operation
query-key-factory copied to clipboard

New createMutationKeys doesn't work for mutations with arguments

Open brmenchl opened this issue 1 year ago • 6 comments

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.

brmenchl avatar Mar 15 '23 18:03 brmenchl