Merging Routers
Writing all API-code in your code in the same file is not a great idea. It's easy to merge routers with other routers.
Merging with child routers​
server.tsts
// @filename: trpc.tsimport {initTRPC } from '@trpc/server';constt =initTRPC .create ();ÂÂexport constmiddleware =t .middleware ;export constrouter =t .router ;export constpublicProcedure =t .procedure ;Â// @filename: routers/_app.tsimport {router } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =router ({user :userRouter , // put procedures under "user" namespacepost :postRouter , // put procedures under "post" namespace});Âexport typeAppRouter = typeofappRouter ;ÂÂ// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({create :publicProcedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),list :publicProcedure .query (() => {// ...return [];}),});Â// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({list :publicProcedure .query (() => {// [..]return [];}),});Â
server.tsts
// @filename: trpc.tsimport {initTRPC } from '@trpc/server';constt =initTRPC .create ();ÂÂexport constmiddleware =t .middleware ;export constrouter =t .router ;export constpublicProcedure =t .procedure ;Â// @filename: routers/_app.tsimport {router } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =router ({user :userRouter , // put procedures under "user" namespacepost :postRouter , // put procedures under "post" namespace});Âexport typeAppRouter = typeofappRouter ;ÂÂ// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({create :publicProcedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),list :publicProcedure .query (() => {// ...return [];}),});Â// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({list :publicProcedure .query (() => {// [..]return [];}),});Â
Merging with t.mergeRouters
​
If you prefer having all procedures flat in one single namespace, you can instead use t.mergeRouters
server.tsts
// @filename: routers/_app.tsimport {router ,publicProcedure ,mergeRouters } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =mergeRouters (userRouter ,postRouter )Âexport typeAppRouter = typeofappRouter ;Â// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({postCreate :publicProcedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),postList :publicProcedure .query (() => {// ...return [];}),});ÂÂ// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({userList :publicProcedure .query (() => {// [..]return [];}),});Â
server.tsts
// @filename: routers/_app.tsimport {router ,publicProcedure ,mergeRouters } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =mergeRouters (userRouter ,postRouter )Âexport typeAppRouter = typeofappRouter ;Â// @filename: routers/post.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constpostRouter =router ({postCreate :publicProcedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),postList :publicProcedure .query (() => {// ...return [];}),});ÂÂ// @filename: routers/user.tsimport {router ,publicProcedure } from '../trpc';import {z } from 'zod';export constuserRouter =router ({userList :publicProcedure .query (() => {// [..]return [];}),});Â