mutationOptions with queryClient variables type inferrence
i made a wrapper function for mutations that lets you create mutationOptions with queryClient in it that you can use for optimistic queries and invalidation, etc... For some reason it can't infer the
variables properly in onMutate, while when i make a wrapper without the queryClient it can:
4 Replies
correct-apricotOP•7mo ago
example usage:
if I remove onMutate, types get inferred correctly, but when I add it, types break with
newTask being inferred as void and mutationFn arguments being inferred as void as well
if I use the mutationOptions variant i made above, then everything (all generics) is properly inferred (though I dont have the queryClient instance to use)adverse-sapphire•7mo ago
Aside from the type issues, using the
useQueryClient() hook in a non-hook helper sounds like a bad idea
And perhaps useful for your issue at hand, I'm using the following useMutation() wrapper that does seem to type the options (including onMutate) correctly, not sure if it helps you in any way
correct-apricotOP•7mo ago
ye i know, though i always call it inside useMutation so its not a problem
sadly no, the issue is that the queryClient seems to modify the type inside the function which is why I get the void type for variables
in the end, I'll probably just go back to extracting the useMutation hook into a custom hook for each mutation, though the idea was to have queryClient always accessible as a parameter in the callback, without having to import and call useQueryClient
adverse-sapphire•7mo ago
Yeah - I tried various things like that too initially but it's a lot of hassle to get that right, gave up in the end