trpc-tanstack-integration
SKILL.md
tRPC + TanStack Query Integration
Goal
Set up end-to-end type-safe API integration using:
@trpc/server@trpc/client@trpc/tanstack-react-query@tanstack/react-query- optional transformer (
devalueorsuperjson)
This skill should perform implementation, not just planning.
Bundled Reusable Templates
Use the prebuilt templates in this skill as the default implementation source.
templates/next-app-router/server/trpc.tstemplates/next-app-router/server/app-router.tstemplates/next-app-router/app/api/trpc/[trpc]/route.tstemplates/next-app-router/client/query-client.tstemplates/next-app-router/client/trpc-react.tsxtemplates/next-app-router/server/trpc-rsc.tsxtemplates/next-app-router/README.md
When integrating, copy these files into the target project and adapt import paths and auth session typing.
Workflow
- Detect project shape before editing.
- Check framework (
next,vite,react,node) frompackage.json. - Detect package manager from lockfiles (
pnpm-lock.yaml,package-lock.json,yarn.lock,bun.lockb). - Detect routing/runtime style (Next.js App Router vs Pages Router vs SPA).
- Install required dependencies.
- Runtime deps:
@trpc/server@trpc/client@trpc/tanstack-react-query@tanstack/react-queryzoddevalue
- Add framework-specific deps only if needed by detected architecture.
- Copy templates first.
- For Next.js App Router, copy files from
templates/next-app-router/. - Follow
templates/next-app-router/README.mdfor destination mapping. - After copying, update imports and aliases to match the target project.
- Create server foundation.
- Add
trpcinitializer with:- context typing
router,publicProcedure, optionalprotectedProcedure- consistent error formatting for Zod validation issues
- transformer serialize/deserialize setup
- Add root router (
appRouter) and exportedAppRoutertype.
- Expose server endpoint.
- For Next.js App Router: create
app/api/trpc/[trpc]/route.tsusingfetchRequestHandler. - Build context per request (auth/session if available).
- Add structured logging for non-auth errors.
- Create TanStack Query client factory.
- Add a reusable
makeQueryClient()with sane defaults:- non-zero
staleTime - hydration/dehydration serializer support
- query/mutation error handling hooks
- non-zero
- Export inferred API input/output types from
AppRouterif helpful.
- Create client provider.
- Implement a
TRPCReactProvideras a client component. - Wrap app tree with:
QueryClientProviderTRPCProviderfromcreateTRPCContext<AppRouter>()
- Use browser singleton query client to avoid recreation on suspense.
- Add server-side helper (when SSR/RSC exists).
- Provide RSC helper utilities for:
- prefetching query options
- creating server-side caller
- hydration boundary wrapper
- Use request-aware context/session where auth exists.
- Wire providers into app layout.
- Ensure root layout or app provider tree includes the TRPC/TanStack provider.
- Preserve existing provider order and do not break existing context dependencies.
- Validate integration.
- Run type-check/lint/build commands available in repo.
- Confirm one read query and one mutation compile and run.
- If tests exist for API layer, run relevant subset.
Implementation Rules
- Prefer adapting existing project conventions (logger, auth, folder layout) over introducing new patterns.
- Reuse existing serialization approach if project already uses one.
- Avoid replacing unrelated files; keep edits minimal and additive.
- If authentication exists, include
protectedProcedure; otherwise keep public-only setup. - If project already has partial integration, complete missing pieces instead of duplicating files.
Next.js App Router Reference Layout
Use this structure when no established convention exists:
src/server/trpc.tssrc/server/routers/_app.tsapp/api/trpc/[trpc]/route.tssrc/lib/trpc-client.tsxsrc/lib/query-client.tssrc/lib/trpc-rsc.tsx
Completion Checklist
- Template files copied from this skill and adjusted for target project paths.
- Dependencies installed.
AppRouterexported.- API route handler wired.
- React Query and TRPC provider mounted in app tree.
- Client can call at least one procedure with inferred types.
- Type-check passes.
- No duplicate provider/query-client instances introduced.
Weekly Installs
4
Repository
comradesharf/se…n-skillsFirst Seen
9 days ago
Security Audits
Installed on
opencode4
gemini-cli4
github-copilot4
codex4
kimi-cli4
cursor4