การรวมและการซ้อนเราเตอร์
เรียนรู้การจัดระเบียบ API ของ tRPC ที่เติบโตขึ้นด้วยการประกอบเราเตอร์ย่อยหลายตัวเข้าเป็นเราเตอร์แอปแบบซ้อนหนึ่งตัว
การรวมและการซ้อนเราเตอร์ เป็นบทเรียน tRPC End-to-End Type Safe APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เรียนรู้ tRPC End-to-End Type Safe APIs ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 10
- บทเรียน
- 40
คำถามที่พบบ่อย
บทเรียน “การรวมและการซ้อนเราเตอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรวมและการซ้อนเราเตอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส tRPC End-to-End Type Safe APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรวมและการซ้อนเราเตอร์”
เรียนรู้การจัดระเบียบ API ของ tRPC ที่เติบโตขึ้นด้วยการประกอบเราเตอร์ย่อยหลายตัวเข้าเป็นเราเตอร์แอปแบบซ้อนหนึ่งตัว คุณปฏิบัติ tRPC End-to-End Type Safe APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน tRPC End-to-End Type Safe APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน tRPC End-to-End Type Safe APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การรวมและการซ้อนเราเตอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน tRPC End-to-End Type Safe APIs นี้ได้ไหม
ได้ บทเรียน tRPC End-to-End Type Safe APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดโครงสร้างด้วยเราเตอร์ tRPC
- การใช้งานกระบวนงานคำค้น
- การพัฒนากระบวนงานการกลายข้อมูล
- การรวมและการซ้อนเราเตอร์