Modules and the Dependency Graph¶
How @RootModule and @Module compose an application graph: what each module sees, the order they are created in, and how module paths prefix routes.
A module is the unit of composition in Typeix. Its metadata declares four things: the controllers it owns, the providers it creates, the modules it imports, and which of its providers it exports. From that, the framework builds a graph and creates every module instance at boot.
The metadata¶
@Module({
imports: [DatabaseModule],
providers: [UserService],
exports: [UserService],
controllers: [UserController],
path: "/v1"
})
class UsersModule {}
| Key | Meaning |
|---|---|
imports | modules whose exports this module may see |
providers | providers created in this module |
exports | providers other modules may see through imports |
controllers | controllers owned by this module, hence its routes |
path | prefix applied to this module's own routes |
@RootModule is the entry point and adds one key, shared_providers — providers visible to every module without an import. There is exactly one root per application.
What a module can see¶
Visibility is explicit, and that is the point of modules. Inside UsersModule, a token resolves if it is any of:
- declared in this module's
providers(the local declaration wins on collision), - exported by a module listed in
imports, - listed in the root module's
shared_providers.
Nothing else. A sibling module's providers are invisible unless the shared ancestor re-exports them. Re-exporting is explicit: to pass an import on, name it in your own exports.
@Module({
imports: [UsersModule],
exports: [UserService], // re-exported to whoever imports this module
providers: []
})
class PlatformModule {}
Creation order¶
The injector walks the graph depth first, left to right, so the deepest dependencies are created before the modules that need them:
A H
/ \ |
C E I
\ / |
D G
| /
B /
\ /
F
Creation order is A → C → E → D → B → H → I → G → F. Modules are created once; a diamond (two modules importing the same third) does not duplicate it.
Module paths compose with routes, but do not stack¶
The route a controller answers is the concatenation of three parts:
module path + controller path + method path
@Module({path: "/v1", controllers: [UserController]})
class V1Module {}
@Controller({path: "/users"})
class UserController {
@GET("/<id>") // → GET /v1/users/<id>
find() {}
}
Empty segments are dropped, so path: "/" on a controller adds nothing. A module's path prefixes only its own controllers: an imported module's path is not prepended to its children's paths. This is what makes imports: [V1Module, V2Module] serve /v1/users and /v2/users side by side on one server.
Why one root per server¶
pipeServer builds one module graph per call and attaches one router to the server. Calling it twice on the same server does not merge the two graphs: both routers listen for request, and whichever does not match ends the response with a 404. Versioning belongs in module path, not in a second pipeServer call.
Feature packages follow the same rule — they are attached to the same graph and server, in order:
await pipeServer(server, Application);
await pipeSwagger(server, Application);
await pipeGraphQL(server, Application);
Modules versus providers: a quick decision table¶
| You are adding… | Put it in |
|---|---|
| a route answering HTTP | a controller in some module's controllers |
| business logic used by routes | a module providers entry, exported if shared |
| something every module needs | root module shared_providers |
| a group of routes for one feature | a feature module, imported by the root |
| a versioned API surface | a module with a path, imported by the root |
| per-request state | @Controller({providers}), not a module |
Further reading¶
- Module API,
ModuleInjector, import/export resolution:@typeix/modules - Practical module walkthrough: Resty modules
- Provider kinds and lifetimes: Providers and scopes