合并与嵌套路由器
学习通过组合多个子路由器构建单一的嵌套应用路由器,从而组织不断增长的 tRPC API。
合并与嵌套路由器 是 CoddyKit 上的免费 tRPC End-to-End Type Safe APIs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 tRPC End-to-End Type Safe APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 tRPC End-to-End Type Safe APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Split Routers?
As your API grows, one giant router becomes hard to read. tRPC lets you build small focused sub-routers and merge them.
A Users Sub-Router
Group all user-related procedures together.
export const userRouter = router({
list: publicProcedure.query(() => getUsers()),
byId: publicProcedure.input(z.string()).query(({ input }) => getUser(input)),
});A Posts Sub-Router
Define another router for posts.
export const postRouter = router({
list: publicProcedure.query(() => getPosts()),
create: publicProcedure.input(z.object({ title: z.string() })).mutation(({ input }) => addPost(input)),
});Nesting Routers
Combine sub-routers under namespaces in the main router.
export const appRouter = router({
user: userRouter,
post: postRouter,
});Calling Nested Procedures
The client path mirrors the nesting structure.
await client.user.list.query();
await client.post.create.mutate({ title: "Hi" });Deep Nesting
You can nest several levels for clear domains.
export const appRouter = router({
admin: router({
user: userRouter,
report: reportRouter,
}),
});Type Inference Flows Through
No matter how deeply you nest, full type inference and autocomplete reach every procedure on the client.
Merging vs Nesting
Nesting puts a router under a namespace key. Merging with t.mergeRouters flattens two routers into one with no extra namespace.
const merged = t.mergeRouters(userRouter, postRouter);When to Merge
Merge when procedures naturally belong at the same level; nest when you want clear domain prefixes like user.* and post.*.
Avoiding Name Collisions
Merging two routers that share a procedure name causes a conflict. Nesting avoids this because each lives under its own namespace.
Keeping Files Small
Place each sub-router in its own file and import them into one root router file. This keeps modules focused and makes the API structure easy to navigate.
Quick Check
Test your router composition knowledge.
Recap
You learned to scale a tRPC API cleanly:
- Build small sub-routers per domain
- Nest them under namespaces for clear paths
- Merge when procedures share a level
Composition keeps large APIs organized while preserving full type safety.
常见问题解答
「合并与嵌套路由器」课时是免费的吗?
是的 — 「合并与嵌套路由器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 tRPC End-to-End Type Safe APIs 课程的其余内容,请升级到 CoddyKit PRO。 tRPC End-to-End Type Safe APIs 课程共包含 4 节课。
「合并与嵌套路由器」这节课中我会学到什么?
学习通过组合多个子路由器构建单一的嵌套应用路由器,从而组织不断增长的 tRPC API。 你通过在浏览器中直接运行的动手代码来练习 tRPC End-to-End Type Safe APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 tRPC End-to-End Type Safe APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 tRPC End-to-End Type Safe APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「合并与嵌套路由器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 tRPC End-to-End Type Safe APIs 课中编写并运行代码吗?
能。每节 tRPC End-to-End Type Safe APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 tRPC 路由器组织代码
- 实现查询过程
- 开发变更过程
- 合并与嵌套路由器