mjdi docsv0.10.0latest
Dependency graph export (P1-01)
graph_json and graph_dot export a registry's dependency graph as
Source: mjdi/docs/graph-export.md
graph_json and graph_dot export a registry's dependency graph as
deterministic, machine-readable output. Both are compile-time walks: the
result is a constant string, with zero runtime cost, and byte-for-byte
deterministic per registry type.
from mjdi import Registry, graph_json, graph_dot
comptime reg = (
Registry()
.register_value[AppConfig]()
.register[UserRepository, provider_scope.singleton]()
.register[UserService]()
)
print(graph_json[type_of(reg)]()) # schema mjdi.graph/1
print(graph_dot[type_of(reg)]()) # Graphviz DOT
Run the export runner for the checked-in showcase fixtures:
mojo run -I . tools/graph_export.mojo showcase json
mojo run -I . tools/graph_export.mojo showcase dot
What the JSON export contains
Schema mjdi.graph/1 (see docs/graph-visualization.md for the viewer
that consumes it and scripts/graph_diff.py for the diff utility):
{
"schema": "mjdi.graph/1",
"nodes": [
{"service": "UserRepository", "provider": "class",
"registration": "register[T]", "lifetime": "singleton",
"alias": false, "alias_target": null, "linear": false,
"copyable": true, "scope_required": false,
"valid": true, "problems": []}
],
"edges": [
{"consumer": "UserService", "dependency": "UserRepository",
"relation": "inject", "lifetime": "singleton",
"provider": "class", "provided": true}
]
}
Nodes identify, per registered provider:
- the service type name,
- the provider kind (
class/value/factory/alias), - the registration source — which registration method produced the
node (
register[T],register_value[T],use_value[T],register_factory[T, factory],use_provider[T, factory],register_alias[From, To],register[T] (linear overload), or theoverridefamily), - the effective lifetime — the lifetime a resolution observes (an
alias inherits its target's lifetime through the chain, mirroring
resolution dispatch, including
overrideprecedence), - whether the node is an alias and its alias target,
- whether the service is a linear service and whether it is Copyable (the cached-lifetime requirement),
- whether a Scope is required to resolve it,
- the validation status plus a machine-readable problems list.
Edges identify consumer, dependency, relation (inject or alias),
the dependency's provider kind and effective lifetime, and whether the
dependency is provided.
Exporting invalid graphs (the important property)
Export never validates. Container construction rejects missing
providers, cycles, and lifetime conflicts at compile time — but you may
want to see the broken graph before writing the composition root.
graph_json walks the same conditions without asserting, so an invalid
registry exports with the offending nodes and edges marked:
{"service": "OrderService", ..., "valid": false,
"problems": ["missing dependency: Payments", "cycle: A -> B -> A"]}
The graph viewer highlights these; the diff tool reports them. When the
registry is valid (or a Container has been constructed), every node
carries "valid": true, "problems": [].
Determinism
Nodes and edges are emitted in registration order (the oldest
provider first). Type names are the reflection-derived names the
diagnostics use (module suffix stripped, parameters kept). The same
registry type always produces the same bytes — the checked-in fixtures
(tests/fixtures/graph_*.json, regenerated by
scripts/export_graph_fixtures.sh) and the CI drift gate rely on it.
The DOT export
graph_dot renders the same graph as Graphviz:
digraph mjdi {
rankdir=LR;
node [shape=record, style="rounded,filled", fontname="Helvetica"];
"UserRepository" [label="{UserRepository|class|singleton}", fillcolor="#dae8fc", style="rounded,filled"];
"UserService" -> "UserRepository";
}
- fill colors distinguish provider kinds and lifetimes
(value
#fff2cc, factory#e1d5e7, alias#ffe6cc; class: transient#f5f5f5, singleton#dae8fc, scoped#d5e8d4), - invalid nodes get a red border (
color="#b85450", penwidth=2), - aliases render with dashed borders and dashed edges,
- linear services render as hexagons.
Related
- Graph visualization — the interactive viewer that consumes these exports.
- Graph diff — before/after comparison for CI.
- Explain resolution — the per-service explanation built from the same metadata.