-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
DRAFT/STRAWMAN/not currently intended for merging: add trpc.foo.useQuery back? #6563
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
base: main
Are you sure you want to change the base?
Conversation
This PR was not deployed automatically as @mmkal does not have access to the Railway project. In order to get automatic PR deploys, please add @mmkal to the project inside the project settings page. |
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
@trpc/client
@trpc/next
@trpc/server
@trpc/react-query
@trpc/tanstack-react-query
@trpc/upgrade
commit: |
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.
Thanks for the PR, good way to start a more formal discussion!
The new structure of the new package allows for introducing this without much overhead so maintenance wise this would be fine.
One problem is we'd need to reintroduce multiple init methods in order to not expose react hooks for server callers for example?
My intuition says to promote trpc.greeting.useQuery() by default, and suggest useQuery(trpc.greeting.queryOptions()) as an escape hatch
My main problem with this is you're leading developers down a "bad path" since they would not benefit from eslint rules nor the compiler, two aspects that this new API that isnt talked about
We'll definitely discuss this, both internally and on this PR though.
@@ -87,6 +93,8 @@ export interface DecorateQueryProcedure<TDef extends ResolverDef> { | |||
*/ | |||
queryOptions: TRPCQueryOptions<TDef>; | |||
|
|||
useQuery: TRPCUseQueryResult<TDef>; |
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.
We could flag this so it's not exposed by default, and people coming from the old API could enable to have a more familiar API.
Something like:
createTRPCOptionsProxy<AppRouter, {
enableOldHooks: true
}>()
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.
Yeah I guess, but as you pointed out in #6508 (reply in thread) there is already away to get the old API, if it is indeed considered "old". My hope with this is to figure out if we can keep around the old API for most use cases indefinitely, because it's better DX. But if that were the direction making it opt in and using the label "old" wouldn't make sense.
If we'd just be adding it to help people migrate extra slowly, then yeah that could be sensible.
Just to echo what Julius is saying, the old integration doesn't look like it's going to age well when React Compiler comes out, and there's a long-standing issue where eslint's hooks rules don't lint it because the rules don't consider our proxy to be valid. The classic client permits (and in the case of our polymorphism work actively encourages) patterns which break the rules of hooks, so will not compile in the future, and we should have emphasised that in the announcement post. I would love to find a way to support custom helpers where teams and codebases want these sorts of wrappers (at their own peril) but I personally think the core surface area of the TRQ client should avoid anything that could become a foot gun in the wrong hands |
@@ -1,4 +1,9 @@ | |||
import type { DataTag, QueryClient, QueryFilters } from '@tanstack/react-query'; | |||
import { | |||
useQuery, |
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.
This might be rejected by Next.js if using server components like this:
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.
Ah I see. I just checked and it seems you're right, that useQuery.ts
has a 'use client'
directive: https://github.com/TanStack/query/blob/74db58292e0b4e4bcf6d8ff720c6770df26a26df/packages/react-query/src/useQuery.ts#L1
Which is a bit of a surprise to me, I'd have thought it'd be left to the user to avoid this kind of problem. Not sure if that could be changeable in tanstack, but I imagine they have thought it through much more than I have.
Re compiler and eslint: this is scope creep, but I just pushed a change which wraps ![]() It's a hack, which depends on the implementation of RuleOfHooks.ts, clearly a better solution would be an actual resolution to facebook/react#25065. But, I also was able to make a very small modification to I don't know about React Compiler, but maybe something similar would be possible? |
So you can get an early view on what's acceptable with https://www.npmjs.com/package/eslint-plugin-react-compiler - it's been a while since I tried it but my work codebase uses tRPC's polymorphism and because it involves passing hooks through props it unfortunately rejects that code. Most tRPC code might go unrejected though I'm not sure if anything would de-opt under the hood because of the chained hooks |
Related to #6240 - CC @juliusmarminge
This PR isn't really a "pull" request - just as a place to allow discussion with commenting on code inline etc. if it's helpful.
Since @KATT suggested something to fill the gap between the old react-query API and the new one might be welcome, I thought I'd just poke around and see what the dumbest possible implementation of it might look like.
I want to be clear though that I'm aware I wasn't involved in the discussions around the new API and what the specific pitfalls of the old one were, so I am very sorry if this is just prompting people to repeat themselves!
🎯 Changes
For now, this just adds a
useQuery(...)
method to theDecorateQueryProcedure
interface. Something similar would be needed foruseMutation
anduseSubscription
anduseInfiniteQuery
, but I didn't bother with those because I wanted to sanity check/find out pitfalls first.DX for consumers wise, I think it brings back pretty much the usage pattern as before, along with all the shortcomings about new react-query features and so on.
Maintenance wise:
Code: it doesn't seem bad? I haven't been in the weeds in trpc for a while so I don't remember which helpers came into existence and when, but all I really did was copy-paste the
TRPCQueryOptions
interface into aTRPCUseQueryResult
interface, with identical generics, but made the return types of each overloadUseQueryResult<TData, TRPCClientErrorLike...>
. The runtime implementation was trivial - just do what we're already doing forqueryOptions
and pass it to auseQuery
imported from@tanstack/react-query
Dependencies: it's worth noting that the import from
@tanstack/react-query
is going fromimport type {...}
toimport {...}
because we're now importinguseQuery
.Of course even if this approach, there's more to consider than just the code. Test and docs would of course be needed, and decisions needed about the docs. trpc would now need to decide when it promotes which variant. My intuition says to promote
trpc.greeting.useQuery()
by default, and suggestuseQuery(trpc.greeting.queryOptions())
as an escape hatch, for when you need to override one of the options, use a new react-query feature, or use a different react-query version.Also, it's hard to predict what'll happen in future. But, if react-query came out with a new version that renamed
queryFn
toqueryFunction
or something, I think it'd be simple for users to do something like"react-query-v123": "npm:@tanstack/react-query@^123.0.0"
in their package.json, then something like this in their code: