03: trpc setup - #3
Conversation
WalkthroughThis PR introduces a tRPC-based data fetching architecture with server-side routing and client-side React Query integration. New dependencies enable Changes
Sequence DiagramsequenceDiagram
participant Server as Server<br/>(Next.js)
participant QueryCache as Query<br/>Cache
participant Hydration as Hydration<br/>Boundary
participant Client as Client<br/>Component
participant TRPCLink as TRPC<br/>Client
Server->>QueryCache: getQueryClient() + trpc.getUsers.prefetch()
QueryCache-->>Server: Cached results
Server->>Hydration: dehydrate(queryClient)
Server->>Client: Render with dehydrated state
Client->>Hydration: Wrap Client in HydrationBoundary
Hydration->>Client: Provide dehydrated state + Suspense
Client->>TRPCLink: useTRPC + useSuspenseQuery
TRPCLink->>Client: Hydrate from cache (or fetch if stale)
Client-->>Server: Render users
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~60 minutes
Areas requiring extra attention:
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
|
Note Docstrings generation - SUCCESS |
Docstrings generation was requested by @AMMAR-62. * #3 (comment) The following files were modified: * `src/app/layout.tsx` * `src/trpc/client.tsx` * `src/trpc/query-client.ts`
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (7)
src/trpc/routers/_app.ts (1)
4-7: Make the query handler async for clarity.While tRPC handles Promises automatically, explicitly marking the Prisma query as async improves code clarity and aligns with Prisma best practices.
🔎 Proposed fix
getUsers: baseProcedure - .query(() => { - return prisma.user.findMany(); + .query(async () => { + return await prisma.user.findMany(); }),src/app/client.tsx (2)
5-5: Remove unused import.The
useStateimport is not used in this component.🔎 Proposed fix
-import { useSuspenseQuery } from "@tanstack/react-query"; -import { useState } from "react"; +import { useSuspenseQuery } from "@tanstack/react-query";
7-7: Fix spacing in function declaration.Add proper spacing around the assignment operator.
🔎 Proposed fix
-export const Client= () => { +export const Client = () => {src/trpc/query-client.ts (1)
5-5: Consider enabling superjson for complex data types.The commented-out superjson setup suggests it may be needed in the future. Without a serializer, complex data types (Date, Map, Set, BigInt, etc.) won't serialize correctly between server and client.
If you need to serialize complex types, install and enable superjson:
npm install superjsonThen uncomment the serialization lines in this file.
Also applies to: 13-13, 19-19
src/app/page.tsx (1)
3-3: Remove unused import.The
callerimport is not used in this component.🔎 Proposed fix
-import { caller, getQueryClient, trpc } from '@/trpc/server' +import { getQueryClient, trpc } from '@/trpc/server'src/trpc/init.ts (1)
3-8: HardcodeduserIdplaceholder needs implementation.The context currently returns a static
userId: 'user_123'. For production, this should integrate with your authentication system (e.g., session, JWT, NextAuth). Additionally, consider accepting request headers to enable proper auth context:-export const createTRPCContext = cache(async () => { +export const createTRPCContext = cache(async (opts?: { headers?: Headers }) => { /** * @see: https://trpc.io/docs/server/context */ - return { userId: 'user_123' }; + // TODO: Replace with actual auth logic + // const session = await getSession(opts?.headers); + return { userId: 'user_123' }; });Would you like me to open an issue to track implementing proper authentication context?
src/trpc/client.tsx (1)
11-23: Consider adding explicit type forbrowserQueryClient.The module-level singleton pattern is correct for preventing query client recreation during React suspense. Adding an explicit type improves clarity:
-let browserQueryClient: QueryClient; +let browserQueryClient: QueryClient | undefined;This makes the
undefinedinitial state explicit, which better documents the lazy initialization intent.
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (10)
package.jsonsrc/app/api/trpc/[trpc]/route.tssrc/app/client.tsxsrc/app/layout.tsxsrc/app/page.tsxsrc/trpc/client.tsxsrc/trpc/init.tssrc/trpc/query-client.tssrc/trpc/routers/_app.tssrc/trpc/server.tsx
🧰 Additional context used
🧬 Code graph analysis (8)
src/trpc/routers/_app.ts (1)
src/trpc/init.ts (2)
createTRPCRouter(20-20)baseProcedure(22-22)
src/trpc/server.tsx (4)
src/trpc/query-client.ts (1)
makeQueryClient(6-23)src/trpc/client.tsx (1)
createTRPCContext(10-10)src/trpc/init.ts (1)
createTRPCContext(3-8)src/trpc/routers/_app.ts (1)
appRouter(3-8)
src/app/client.tsx (1)
src/trpc/server.tsx (1)
trpc(10-14)
src/trpc/init.ts (1)
src/trpc/client.tsx (1)
createTRPCContext(10-10)
src/trpc/client.tsx (4)
src/trpc/init.ts (1)
createTRPCContext(3-8)src/trpc/routers/_app.ts (1)
AppRouter(10-10)src/trpc/server.tsx (1)
getQueryClient(9-9)src/trpc/query-client.ts (1)
makeQueryClient(6-23)
src/app/layout.tsx (1)
src/trpc/client.tsx (1)
TRPCReactProvider(32-59)
src/app/page.tsx (3)
src/trpc/server.tsx (2)
getQueryClient(9-9)trpc(10-14)src/lib/utils.ts (1)
cn(4-6)src/app/client.tsx (1)
Client(7-16)
src/app/api/trpc/[trpc]/route.ts (3)
src/trpc/routers/_app.ts (1)
appRouter(3-8)src/trpc/client.tsx (1)
createTRPCContext(10-10)src/trpc/init.ts (1)
createTRPCContext(3-8)
🔇 Additional comments (7)
src/app/layout.tsx (1)
4-4: LGTM! Clean tRPC provider integration.The TRPCReactProvider wrapper is correctly placed in the root layout to provide tRPC context throughout the application.
Also applies to: 31-31
src/app/api/trpc/[trpc]/route.ts (1)
1-11: LGTM! Standard tRPC route handler setup.The fetch adapter is correctly configured with the router and context creator, and properly exported for both GET and POST requests.
src/app/page.tsx (1)
10-20: LGTM! Correct SSR hydration pattern.The prefetch + HydrationBoundary + Suspense setup follows React Query and tRPC best practices for server-side rendering with client-side hydration.
package.json (1)
42-45: Dependency versions are compatible.The tRPC packages (v11.8.1) and @tanstack/react-query (v5.90.12) are compatible with React 19 and Next.js 15, with no known compatibility issues.
src/trpc/init.ts (1)
13-22: LGTM - tRPC initialization follows recommended patterns.The setup correctly:
- Avoids exporting the raw
tobject (as noted in comments)- Exposes granular helpers (
createTRPCRouter,createCallerFactory,baseProcedure)- Leaves room for adding a data transformer when needed
src/trpc/client.tsx (2)
32-59: LGTM - Provider setup follows tRPC + React Query best practices.The implementation correctly:
- Uses
useStatefor stable tRPC client reference (preventing recreation on re-renders)- Applies the query client singleton pattern with proper SSR handling
- Nests providers in the correct order (
QueryClientProvider→TRPCProvider)- Includes helpful comments explaining the suspense boundary considerations
24-31: No refactoring is needed. ThegetUrl()function is client-specific and handles SSR scenarios;src/trpc/server.tsxuses server-side routing viaappRouter.createCaller()and does not duplicate this logic.Likely an incorrect or invalid review comment.
Summary by CodeRabbit
New Features
✏️ Tip: You can customize this high-level summary in your review settings.