snöw
snöw14mo ago

Why cannot I specify a mutation input with useQueryKey?

It's supported with queries, but for some reason not with mutations. Why is this the case?
14 Replies
snöw
snöw14mo ago
The use-case is that I want global loading states for mutation with https://tanstack.com/query/v4/docs/react/reference/useIsMutating
useIsMutating | TanStack Query Docs
useIsMutating is an optional hook that returns the number of mutations that your application is fetching (useful for app-wide loading indicators). `tsx
snöw
snöw14mo ago
which I need a key for, hence the need for useQueryKey
Nick
Nick14mo ago
Mutations don’t typically have query keys, they’re not cached This is a react-query thing (I guess you get that though from the second link) Do we not expose isMutating via trpc.useContext? Many things that need query keys we do expose this way If not it might be because trpc.useMutation actually serves the same purpose in the context of trpc
snöw
snöw14mo ago
useContext only gives you the queries, so no luck there
snöw
snöw14mo ago
but I ended up here
snöw
snöw14mo ago
useIsMutating with the predicate where I can access the variables works, but no typesaftey 😦 I guess this could be wrapped to a custom TRPC wrapper of useIsMutating where the type is narrowed based on the mutationKey
Nick
Nick14mo ago
Yeah very possibly could be something we’ve missed Feel free to open a GitHub issue
snöw
snöw14mo ago
I used useIsMutating straight from react-query, so nothing missed 😄 but yep, this can be a feature request
julius
julius14mo ago
iirc we have these in v11 Oh wait you wanna get the mutation key with input Nah
snöw
snöw14mo ago
that was the initial idea, but a custom useIsMutating with the mutation state typed in the predicate would be a better solution
snöw
snöw14mo ago
react-query hard sets the Mutation types to any, so a change would be required there as well
snöw
snöw14mo ago
I had something like this in mind:
snöw
snöw14mo ago
but the as step done by trpc anything specific I could check out?
Riccardo
Riccardo10mo ago
Hey, I need somthing you solved with useIsMutating but with isMutating client helper. I filed an issue https://github.com/trpc/trpc/issues/4889, do you mind bringing some more ideas/support there?