Skip to content

feat(tanstack-react-query): start query handles from a /handles entrypoint - #7764

Closed
KATT wants to merge 15 commits into
mainfrom
cursor/handles-entrypoint-0ee9
Closed

KATT wants to merge 15 commits into
mainfrom
cursor/handles-entrypoint-0ee9

Conversation

@KATT

@KATT KATT commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

An alternative to #7759. It builds on #7759's commits: this compare view shows only what differs. Merge one or the other, not both.

Why

What

A new entrypoint, @trpc/tanstack-react-query/handles, with a proxy that only starts queries:

import { unstable_createTRPCHandleProxy } from '@trpc/tanstack-react-query/handles';

// Same options as createTRPCOptionsProxy: { client, queryClient } or { router, ctx, queryClient }
const trpc = unstable_createTRPCHandleProxy<AppRouter>({ client: trpcClient, queryClient });

// Route loader or server component
const comments = trpc.post.comments.prefetchInfinite({ postId: id, limit: 20 }); // never waits, never throws
const post = await trpc.post.byId.ensure({ id }); // waits for data; stale data resolves at once
const me = await trpc.viewer.fetch(); // waits for fresh data
post.data; // ensure and fetch return the data they waited for
comments.input.postId; // every handle carries its typed input

// Component: unchanged from #7759
const { data } = useSuspenseQuery(useTRPC().unstable_fromHandle(props.post));

// Props
function Post(props: { post: unstable_inferQueryHandle<TRPCHandles['post']['byId']> }) {}
  • The handle proxy has only query procedures. Each has prefetch, ensure and fetch, plus prefetchInfinite, ensureInfinite and fetchInfinite when the input has a cursor. They map to prefetchQuery, ensureQueryData({ revalidateIfStale: true }) and fetchQuery, and to the infinite versions of each.
  • The verbs have no unstable_ prefix. The prefix is on the way in: unstable_createTRPCHandleProxy, unstable_TRPCHandleProxy and the handle types.
  • ensure and fetch resolve to the handle plus data, so a loader can branch on the data and a server component can render it. feat(tanstack-react-query): query handles with $ helpers (unstable_$prefetch, unstable_$ensure, unstable_$fetch, unstable_$fromHandle)Β #7759 left data off handles. The cost is that data is serialized with the handle wherever you pass it, and with a transformer in serializeData it crosses a second time.
  • Every handle has input, typed from the procedure, so a component that gets a handle doesn't also need its input as a prop. Infinite handles leave out cursor and direction, matching the query key; the starting cursor stays in initialCursor. The input is already in the query key, so it now crosses twice; inputs are usually small.
  • unstable_useWaterfall(useTRPC().x, input), from the same entrypoint, is the explicit escape hatch for a query whose input is only known at render time. It starts the query during render and returns a handle, so the reader is the same as for a loader's handle. The name marks every waterfall in a codebase. Query procedures only; the docs import it as useWaterfall for the rules-of-hooks lint rule.
  • The docs gain a Request waterfalls section: start queries in loaders, start every query before awaiting any, make the URL the input, reshape dependent queries in the route or a procedure, and use the two explicit waterfalls (await trpc.x.ensure() then prefetch from its data, or unstable_useWaterfall) only when you can't.
  • The options proxy loses the verbs. It keeps unstable_fromHandle for reading, so useTRPC() no longer has verbs that throw, and disableHandleVerbs is gone.
  • Anything other than a verb on the handle proxy throws a readable error.
  • Exports from @trpc/tanstack-react-query/handles: unstable_createTRPCHandleProxy, unstable_TRPCHandleProxy, unstable_useWaterfall, unstable_TRPCQueryHandle, unstable_TRPCInfiniteQueryHandle, unstable_TRPCQueryHandleWithData, unstable_TRPCInfiniteQueryHandleWithData, unstable_AnyTRPCHandle, unstable_TRPCPrefetchOptions, unstable_TRPCPrefetchInfiniteOptions, unstable_inferQueryHandle, unstable_inferInfiniteQueryHandle. The main entry keeps unstable_TRPCQueryHandle, unstable_TRPCInfiniteQueryHandle and unstable_AnyTRPCHandle for readers.
  • The localLink rename from feat(tanstack-react-query): query handles with $ helpers (unstable_$prefetch, unstable_$ensure, unstable_$fetch, unstable_$fromHandle)Β #7759 is included unchanged.
  • TanStack Start SSE example: the router context holds a handle proxy, and loaders call trpc.channel.list.prefetch() and trpc.post.infinite.prefetchInfinite({ channelId }). Components still read with useTRPC().unstable_fromHandle(...). Chat and useLivePosts take only the posts handle and read channelId from posts.input.

Links

A different root proxy (not in this PR)

This PR adds a proxy that only starts queries, next to createTRPCOptionsProxy. A bigger option is one root proxy that is the whole handle-first API. It's written down here in case we revisit it.

What it would have:

  • The same verbs: trpc.post.byId.prefetch, ensure and fetch.
  • Its own reader, such as useQuery(handle) returning [data, controls] for a handle from ensure or fetch, and [promise, controls] for one from prefetch, instead of useSuspenseQuery(useTRPC().unstable_fromHandle(handle)).
  • Cache verbs keyed by procedure and input, or by handle: invalidate, setData, getData, reset and remove.
  • Mutations as plain calls inside actions (await trpc.post.create.mutate(input), then invalidate), without mutationOptions.
  • A provider of its own, so components get the same object loaders used.

What it would buy:

  • One object and one import for the whole flow. No second proxy, and no unstable_fromHandle plumbing in every component.
  • A reader whose type follows how the query started: data if the loader waited, a promise if it didn't.
  • No way to start a query during render except an explicit escape hatch like unstable_useWaterfall, which keeps request waterfalls out of the default path.

What it would cost:

  • A second full API next to createTRPCOptionsProxy, with its own docs, types, tests and migration guide.
  • tRPC would own the read side: wrapping TanStack's observers for Suspense and controls, and choosing what to drop (select, placeholderData and the status flags).
  • Code that expects queryOptions, such as useQueries or third-party helpers, would still need the options proxy.

When to revisit:

  • When handles are the default in the docs, and useTRPC().unstable_fromHandle(...) reads as boilerplate.
  • When we want the reader's type to depend on the verb, or cache verbs keyed by handle.

This PR doesn't block that path. The handle proxy and its types are already apart from the options proxy, so a reader and cache verbs could join the handles entrypoint without changing createTRPCOptionsProxy.

Testing

  • packages/tanstack-react-query/test/handles.test.tsx (26 tests): feat(tanstack-react-query): query handles with $ helpers (unstable_$prefetch, unstable_$ensure, unstable_$fetch, unstable_$fromHandle)Β #7759's tests, moved to start handles on the handle proxy, plus:
    • ensure and fetch return data with the handle, typed from the procedure's output.
    • Handles carry their typed input, without cursor for infinite handles, and keep it through serialization.
    • The handle proxy has no queryOptions, and prefetchInfinite only exists on procedures with a cursor input.
    • Anything other than a verb throws.
    • unstable_useWaterfall starts the query in render, returns a typed handle, doesn't refetch on re-render, and starts a new query when the input changes.
    • useTRPC() has no verbs in its type.
  • packages/tanstack-react-query/test/handles.ssr.test.tsx: server rendering from the dehydrated state with superjson and Date values, now starting on the handle proxy.
  • pnpm build regenerates the package's exports and turbo.json outputs for ./handles.
  • The package's typecheck, lint and lint-prune pass, and pnpm test -- --watch false passes (1390 tests).
  • TanStack Start SSE example: typecheck, lint and the production build pass. I ran the build against a scratch local Postgres with one seeded channel. / and /channels/<id> returned 200 and server-rendered the channel. The server logged no errors or handle warnings.
Open in WebΒ Open in CursorΒ 

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

cursoragent and others added 10 commits October 31, 2026 23:17
…es; fix ts-expect-error placement

Co-authored-by: alexander <[email protected]>
…ct-query/handles entrypoint

The handle proxy only starts queries: prefetch, ensure and fetch (plus their Infinite forms), without the unstable_ prefix. ensure and fetch return the handle with the data they waited for. Reading stays on the options proxy with unstable_fromHandle.

Co-authored-by: alexander <[email protected]>
@vercel

vercel Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tanstack-start-sse-chat Ready Ready Preview Nov 1, 2026 12:10am UTC
trpc-next-app-dir Ready Ready Preview Nov 1, 2026 12:10am UTC
www Ready Ready Preview Nov 1, 2026 12:10am UTC
4 Skipped Deployments
Project Deployment Actions Updated
examples-todomvc Skipped Skipped Nov 1, 2026 12:10am UTC
next-prisma-starter Skipped Skipped Nov 1, 2026 12:10am UTC
next-prisma-websockets-starter Skipped Skipped Nov 1, 2026 12:10am UTC
og-image Skipped Skipped Nov 1, 2026 12:10am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@pkg-pr-new

pkg-pr-new Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/@trpc/client@7764

@trpc/next

npm i https://pkg.pr.new/@trpc/next@7764

@trpc/openapi

npm i https://pkg.pr.new/@trpc/openapi@7764

@trpc/react-query

npm i https://pkg.pr.new/@trpc/react-query@7764

@trpc/server

npm i https://pkg.pr.new/@trpc/server@7764

@trpc/tanstack-react-query

npm i https://pkg.pr.new/@trpc/tanstack-react-query@7764

@trpc/upgrade

npm i https://pkg.pr.new/@trpc/upgrade@7764

commit: 9a54768

@vercel
vercel Bot temporarily deployed to Preview – examples-todomvc November 1, 2026 00:08 Inactive
@vercel
vercel Bot temporarily deployed to Preview – og-image November 1, 2026 00:08 Inactive
@vercel
vercel Bot temporarily deployed to Preview – next-prisma-starter November 1, 2026 00:08 Inactive
@KATT KATT closed this Nov 1, 2026
@KATT
KATT deleted the cursor/handles-entrypoint-0ee9 branch November 1, 2026 00:50
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 2, 2026

This branch was successfully deployed

2 active and 3 inactive deployments
Preview – www β€” 9a54768a Deployed Nov 1, 2026 by vercel[bot]
Preview – trpc-next-app-dir β€” 9a54768a Deployed Nov 1, 2026 by vercel[bot]
Preview – next-prisma-starter β€” 9a54768a Deployed Nov 1, 2026 by vercel[bot]
Preview – og-image β€” 9a54768a Deployed Nov 1, 2026 by vercel[bot]
Preview – examples-todomvc β€” 9a54768a Deployed Nov 1, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants