Skip to main content

Graph

DiCaf can render a container's bindings as a dependency graph in several formats. All graph functions are exported from the main package.

import {
graphToText,
graphToMarkdown,
graphToMermaid,
graphToDot,
graphToJson,
} from '@caffeine-projects/dicaf'

graphToText

graphToText(input: Iterable<[Key, Binding]>): string

Renders the graph as a plain-text tree. Suitable for terminal output and log files.

console.log(graphToText(container))

Example output:

UserService(scope=singleton)
├─ Logger(scope=singleton)
└─ Database(scope=singleton)

Logger(scope=singleton)

Database(scope=singleton)
└─ ConnectionPool(scope=singleton)

graphToMarkdown

graphToMarkdown(input: Iterable<[Key, Binding]>): string

Renders the graph as a Markdown table. Suitable for embedding in GitHub issues, wikis, and pull request descriptions.

console.log(graphToMarkdown(container))

graphToMermaid

graphToMermaid(input: Iterable<[Key, Binding]>): string

Renders the graph as a Mermaid flowchart diagram. GitHub renders Mermaid blocks natively in Markdown files.

console.log(graphToMermaid(container))

Example output:

```mermaid
%%{init: {"flowchart": {"htmlLabels": false}}}%%
flowchart LR
n0["UserService\nsingleton"]
n1["Logger\nsingleton"]
n0 --> n1
```

graphToDot

graphToDot(input: Iterable<[Key, Binding]>): string

Renders the graph in GraphViz DOT format. Use dot -Tsvg -o graph.svg to render it to an image.

console.log(graphToDot(container))

graphToJson

graphToJson(input: Iterable<[Key, Binding]>): string

Renders the graph as a JSON string. Suitable for custom tooling or storage.

const json = graphToJson(container)