Unire e annidare i router
Impari a organizzare un’API tRPC in crescita componendo più sub-router in un unico app router annidato.
Unire e annidare i router è una lezione tRPC End-to-End Type Safe APIs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento tRPC End-to-End Type Safe APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso tRPC End-to-End Type Safe APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara tRPC End-to-End Type Safe APIs con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 10
- Lezioni
- 40
Domande Frequenti
La lezione «Unire e annidare i router» è gratuita?
Sì — il testo completo di «Unire e annidare i router» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso tRPC End-to-End Type Safe APIs, passa a CoddyKit PRO. Il corso tRPC End-to-End Type Safe APIs include 4 lezioni in totale.
Cosa imparerò in «Unire e annidare i router»?
Impari a organizzare un’API tRPC in crescita componendo più sub-router in un unico app router annidato. Eserciti tRPC End-to-End Type Safe APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare tRPC End-to-End Type Safe APIs?
Non è richiesta alcuna esperienza precedente. tRPC End-to-End Type Safe APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Unire e annidare i router»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione tRPC End-to-End Type Safe APIs?
Sì. Ogni lezione tRPC End-to-End Type Safe APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Strutturare il codice con i router tRPC
- Implementare procedure di query
- Sviluppare procedure di mutation
- Unire e annidare i router