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.
Thanks to TypeScript 4.1 template literal types we can also prefix the procedures without breaking typesafety.
Merging with child routers​
server.tsts
// @filename: routers/_app.tsimport {t } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =t .router ({user :userRouter , // put procedures under "user" namespacepost :postRouter , // put procedures under "post" namespace});Âexport typeAppRouter = typeofappRouter ;ÂÂ// @filename: routers/post.tsimport {t } from '../trpc';import {z } from 'zod';export constpostRouter =t .router ({create :t .procedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),list :t .procedure .query (() => {// ...return [];}),});Â// @filename: routers/user.tsimport {t } from '../trpc';import {z } from 'zod';export constuserRouter =t .router ({list :t .procedure .query (() => {// [..]return [];}),});Â
server.tsts
// @filename: routers/_app.tsimport {t } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =t .router ({user :userRouter , // put procedures under "user" namespacepost :postRouter , // put procedures under "post" namespace});Âexport typeAppRouter = typeofappRouter ;ÂÂ// @filename: routers/post.tsimport {t } from '../trpc';import {z } from 'zod';export constpostRouter =t .router ({create :t .procedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),list :t .procedure .query (() => {// ...return [];}),});Â// @filename: routers/user.tsimport {t } from '../trpc';import {z } from 'zod';export constuserRouter =t .router ({list :t .procedure .query (() => {// [..]return [];}),});Â
Merging with t.mergeRouters
​
If you prefer having all procedures flat in your router, you can instead use t.mergeRouters
server.tsts
// @filename: routers/_app.tsimport {t } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =t .mergeRouters (userRouter ,postRouter )Âexport typeAppRouter = typeofappRouter ;Â// @filename: routers/post.tsimport {t } from '../trpc';import {z } from 'zod';export constpostRouter =t .router ({postCreate :t .procedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),postList :t .procedure .query (() => {// ...return [];}),});ÂÂ// @filename: routers/user.tsimport {t } from '../trpc';import {z } from 'zod';export constuserRouter =t .router ({userList :t .procedure .query (() => {// [..]return [];}),});Â
server.tsts
// @filename: routers/_app.tsimport {t } from '../trpc';import {z } from 'zod';Âimport {userRouter } from './user';import {postRouter } from './post';ÂconstappRouter =t .mergeRouters (userRouter ,postRouter )Âexport typeAppRouter = typeofappRouter ;Â// @filename: routers/post.tsimport {t } from '../trpc';import {z } from 'zod';export constpostRouter =t .router ({postCreate :t .procedure .input (z .object ({title :z .string (),}),).mutation (({input }) => {// [...]}),postList :t .procedure .query (() => {// ...return [];}),});ÂÂ// @filename: routers/user.tsimport {t } from '../trpc';import {z } from 'zod';export constuserRouter =t .router ({userList :t .procedure .query (() => {// [..]return [];}),});Â