lukemorales / query-key-factory

A library for creating typesafe standardized query keys, useful for cache management in @tanstack/query
https://www.npmjs.com/package/@lukemorales/query-key-factory
MIT License
1.16k stars 32 forks source link

Support useMutation #49

Closed mc-petry closed 1 year ago

mc-petry commented 1 year ago

I want to be able to do something like that:

const users = createQueryKeys('users', {
  list: null,
  editing: (id: string) => ({
    mutationKey: [id],
    mutationFn: ...
  }),
})

const { isLoading, mutate } = useMutation({
   ...users.editing(data.id),
   onSuccess() {}
})

I needed this to be able to use useIsMutating hook.

lukemorales commented 1 year ago

This shouldn't be very complicated to implement, we only need to expose a new function createMutationKeys with the same signature as createQueryKeys but changing queryFn and queryKey to mutationFn and mutationKey. Would you like to contribute to that with a PR @mc-petry ?

lukemorales commented 1 year ago

@mc-petry support for mutations is now available in https://github.com/lukemorales/query-key-factory/releases/tag/v1.2.0

Shoutout to @s-bondarenko for the contribution!

tastyqbit commented 1 year ago

Hi @lukemorales, would it be possible to create an example in the readme, I am unsure of how to use this in the way I would like - my goal is to use the new createMutationKeys to automatically update state that instead of manually having to use setQueryData and onSuccess in the useMutation function

Is that possible?