RFC: New Tanstack Integration #6240
Replies: 15 comments 24 replies
-
Beta Was this translation helpful? Give feedback.
-
|
The big question for me is where we draw lines on verbosity, React Query can be quite verbose on its own and typically wrapped inside named hooks when it's all used manually, but tRPC usage should feel seamless and balance internal complexity with external complexity This is a good example of where we have tradeoffs to make:
Option C is probably too far since it's wrapping React Query but is easily the nicest to use, option B uses a standard interface which can be used in a few QueryClient methods. Also needing to call both useTRPC and useQueryClient was very quickly an annoyance for me when updating tests and so I've played with exposing queryClient directly on the tRPC utils. Bit of a balancing act to create a great DX while also developing a dogma which will guide future API surface area |
Beta Was this translation helpful? Give feedback.
-
|
This might be relevant to this discussion: #5751 At the moment, It's not possible to use browser-side tRPC & TanStack Query, that leads to significant disadvantages. |
Beta Was this translation helpful? Give feedback.
-
|
is |
Beta Was this translation helpful? Give feedback.
-
|
I feel this is the way to go. This will provide more freedom and flexibility to the developers to implement complex use cases (for which raw react query is ideal). However, it would be nice to provide code snippets/helper functions in the docs that can be copied/pasted (like shadcn) to simplify the API for the most common use cases. That way, the library remains flexible and easy to evolve, and we could still avoid some verbosity. |
Beta Was this translation helpful? Give feedback.
-
|
The current implementation works 90% of the time without thought. But that 10% comes with some big pain points (mostly revolving around query keys). I think exposing query options instead of a wrapper would be a big improvement. |
Beta Was this translation helpful? Give feedback.
-
|
I see it as way nicer. Exposing the query keys right also gives the possibility of integrating new tooling around it. Very much in favor of the change. |
Beta Was this translation helpful? Give feedback.
-
|
I think this is great! I like the queryOptions integration, that's how I plugged Convex into React Query in https://github.com/get-convex/convex-react-query. const { isPending, error, data } = useQuery({
...convexQuery(api.repos.get, { repo: "made/up" }),
gcTime: 10000, // unsubscribe after 10s of no use
}); |
Beta Was this translation helpful? Give feedback.
-
|
Does this make porting/updating on third party frameworks like solidjs etc. easier for trpc/tantack query? |
Beta Was this translation helpful? Give feedback.
-
|
This is great and I can't wait to have it! This will make new features like skip token available immediately without having to wait for TRPC to be updated. 👍 One note, I think I might prefer writing "overrides" like this: const mutation = useMutation({
...trpc.myMutation.mutationOptions(),
onSettled: () => trpc.queryClient.invalidateQueries(trpc.myQuery.queryOptions()),
onError: (error) => toast({ description: formatError(error) }),
})(And I can easily do that because the API is very transparent.) |
Beta Was this translation helpful? Give feedback.
-
|
I had a pr open to add the Given how nice the |
Beta Was this translation helpful? Give feedback.
-
|
A new API is promising |
Beta Was this translation helpful? Give feedback.
-
|
I'm apparently the minority but I find the new syntax very unreadable. It feels like most of the lines are distracting implementation details. Even with those very simple examples I find myself reading so many words just to mentally parse that, e.g., I'm just mutating something and refetching a query. Have you guys/gals explored the idea of maybe just exposing queryOptions and mutationOptions as a method on query/mutations so that we can keep the current DX but also allow for an escape hatch for things that aren't yet implemented? Anyway, I totally understand if it's that burdensome to maintain a wrapper package. While I'm at it, thanks to all contributors of this amazing library. |
Beta Was this translation helpful? Give feedback.
-
|
With tanStack query being extended to other platforms such as angular (https://tanstack.com/query/latest/docs/framework/angular/overview) it would be amazing to validate if this approach could be suitable for all platforms or if lightweight adjustments could be provided to make it work in all tanStack query environments. |
Beta Was this translation helpful? Give feedback.
-
|
https://trpc.io/blog/introducing-tanstack-react-query-client |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As tRPC has grown, its types have grown with it. In particular, our React Query integration that we need to keep in sync with updates to the upstream React Query package whoses types are also quite complex with a lot of generics that need to match up. Further, complying to semver on the type level is extremely hard which can lead to breaking changes in patch or minor versions of either package.
Our proposal
To address this, we've been exploring a more minimal integration package that greatly simplifies both runtime and type integration code between the two packages. This new package builds on top of the
queryOptionsfunction introduced not too long ago in React Query. Before we dive in to why, let's look at what will change:Before
After
For reference, this is how raw React Query would look like in this example:
Hold on, this seems like worse DX, no?
At first glance, you probably noticed that this new API is more verbose than previously. While this is true, it comes with a couple of advantages.
Firstly, the main limitation with out current package is that we do not support all of the queryClient methods. While this list has been improved over time, it generally takes user engagement for us to add support for a new method which can be frustrated for users who wants to use something today. Additionally, for each method we implement adds complexity to our already quite gnarly types which can easily touch internal types that might change in minor/patch React Query versions.
With this new package, we remove a lot of the complexity back to being just React Query. You have all their methods available without any abstraction hidden on top. This will also help onboarding new developers to your codebase, as you'll only have to teach them React Query. There'll be no tRPC layer on top. This also means we don't have to document or explain to people how React Query works, as you will use React Query directly.
Next,
eslint-plugin-react-hookshave never been able to detect misuse oftrpc.proc.useQuery, meaning you could call this hook conditionally without any static analysis tool catching it. tRPC also supports inverting the control of components (ie. polymorphism). This will not be possible under React Compiler unless we add these APIs. Calling hooks on props is entirely banned and won't compile with React Forget.Additionally, we believe that with this approach, a new React Query major does not require a new tRPC major, and vice versa.
Lastly, this could potentially also make it trivial to add
@trpc/tanstack-vue-queryor similar in the future.Try it out today!
To make this easy for you to try out and provide your feedback, we added the
queryOptionsmethod to the existing proxy in@trpc/react-query:Note that this is a simple version and only includes
.queryOptions(). You can checkout ourTanstack Start examplefor how it might look to adopt this in your app, as well as the PR implementing the new package.Please let us know your thoughts! We would hate to implement this and for no one to like it. Our plan is to deprecate the current
@trpc/react-querypackage if all goes well, hopefully with codemod for easy migration! This API is not final and we're very much open to suggestions!Beta Was this translation helpful? Give feedback.
All reactions