ルーターの統合とネスト
複数のサブルーターを1つのネストしたアプリルーターに組み合わせ、拡大するtRPC APIを整理する方法を学びます。
「ルーターの統合とネスト」はCoddyKit上の無料tRPC End-to-End Type Safe APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「ルーターの統合とネスト」レッスンは無料ですか?
はい。「ルーターの統合とネスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、tRPC End-to-End Type Safe APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 tRPC End-to-End Type Safe APIsコースには全4レッスンが含まれています。
「ルーターの統合とネスト」で何を学びますか?
複数のサブルーターを1つのネストしたアプリルーターに組み合わせ、拡大するtRPC APIを整理する方法を学びます。 ブラウザで直接実行するハンズオンコードでtRPC End-to-End Type Safe APIsを演習し、24時間対応の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ルーターによる構成
- クエリプロシージャの実装
- ミューテーションプロシージャの開発
- ルーターの統合とネスト