-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat(react-query): add support for mutations to createTRPCQueryUtils
and improve offline support
#5814
base: next
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@simonecervini is attempting to deploy a commit to the trpc Team on Vercel. A member of the Team first needs to authorize it. |
createTRPCQueryUtils
createTRPCQueryUtils
* @link https://tanstack.com/query/latest/docs/reference/QueryClient/#queryclientsetmutationdefaults | ||
*/ | ||
setMutationDefaults: ( | ||
mutationKey: string[][], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this type. string[][]
should be better than MutationKey
from TanStack Query, which is defined as ReadonlyArray<unknown>
. But I am not sure what the relationship between this type and TRPCQueryKey
should be, assuming there should be a relationship
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably [string[]]
is a better choice here (= TRPCQueryKey
without the optional second value of the tuple)
We can also define it as [TRPCQueryKey[0]]
or define a new alias
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 790ec8e
createTRPCQueryUtils
createTRPCQueryUtils
and improve offline support
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, appreciate the time you put into this PR
However, I struggle to find the value added with this. What is trpc specific here? The wrappers you added here doesn't use the procedure input/output in any way afaict, so why can't you just use the queryClient.isMutating()
or similar directly here?
Hey @juliusmarminge
However,
utils.post.create.setMutationDefaults(({canonicalMutationFn}) => ({ mutationFn: canonicalMutationFn })) you should need to write something like this: const mutationKey = getQueryKey(procedure)[0];
queryClient.setMutationDefaults(mutationKey, {
mutationFn: (input: unknown) =>
trpcClient.mutation(mutationKey.join("."), { input }),
}); And you don't get errors if |
Closes #5800
💡 Motivation
This PR is largely motivated by the need to improve offline support in tRPC. According to my research, there were two main missing pieces:
utils.anyRouter.anyMutationProcedure.setMutationDefaults()
mutationFn
withsetMutationDefaults()
I decided to go further and implement all the missing
QueryClient
methods useful to interact with mutations, and I added specific tests for offline scenarios.🎯 Changes
This PR adds to
createTRPCQueryUtils
all the utility functions for managing mutations.There are 5 mutation methods in the
QueryClient
class:The last two methods,
getMutationCache
andresumePausedMutations
, don’t accepts arguments and can be called directly from theQueryClient
instance, so we don’t need to implement them increateTRPCQueryUtils
.✅ Checklist