Skip to content

feat(cli): add dataform compile --mermaid to output the dependency graph as a Mermaid flowchart - #2435

Merged
apilaskowski merged 3 commits into
dataform-co:mainfrom
dmargol1:feat/compile-mermaid-output
Nov 23, 2026
Merged

apilaskowski merged 3 commits into
dataform-co:mainfrom
dmargol1:feat/compile-mermaid-output

Conversation

@dmargol1

@dmargol1 dmargol1 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2434.

Problem

dataform compile --dot output usually has to be turned into an image before it can be shared. GitHub, GitLab and other Mermaid-enabled documentation tools render Mermaid natively in Markdown. The main use case is a filtered diagram pasted where people review changes, e.g. a table and everything upstream of it in a PR description:

dataform compile --mermaid --output-actions=my_table --output-include-deps

Changes

  • --mermaid flag on dataform compile, mutually exclusive with --json and --dot. It shows the same actions and dependencies as --dot, and works with the existing --output-* filters.
  • Output: graph LR, which Azure DevOps wikis also support (they don't support the newer flowchart keyword). Tables are labelled with their table type, and dependencies outside the selection (e.g. declarations) appear as plain nodes.
  • Labels: schema.name, plus the database when several nodes share the same schema.name (targets without a database, e.g. in a project with no defaultDatabase, keep the plain schema.name). Table types are read from the string type when enumType isn't set, as prune() does, so labels are the same with or without --output-* filters. Characters Mermaid would interpret (", #, &, <, >, backticks, backslashes, colons, $) are escaped so they display literally, and control characters are shown as Unicode control pictures (e.g. ␊).
  • Node IDs: n plus 8 hex characters of a SHA-256 hash of the full target, with only colliding IDs lengthened. Unlike --dot, same-named tables in different databases stay separate nodes.
  • Deterministic output: sorted by target, so regenerated diagrams diff cleanly.
  • Size warning on stderr when the diagram exceeds Mermaid's default (configurable) limits of 500 edges or 50,000 characters, suggesting --output-* filters. Stdout and the exit code are unaffected.
  • Code layout: the Mermaid rendering lives in its own cli/mermaid.ts, so cli/console.ts stays focused on console I/O.
  • Docs: the README and help text cover the new option, the mermaid code fence, the size limits, watch-mode output and a filtered example.

Like --dot, the output doesn't include notebooks, data preparations or property graphs. Styles, subgraphs and theming are left for follow-ups.

Why hashed IDs

Mermaid renderers limit diagram size (GitHub refuses diagrams over 50,000 characters), and every edge repeats two IDs. With readable IDs derived from the target name, a 300-table chain came to 73,375 characters; with hashed IDs it's 24,883. For that benchmark, the 500-edge default becomes the limit first, though very long labels can still hit the character limit. Readable IDs could be added later as an opt-in; hashed would stay the default so committed diagrams don't change.

Example

graph LR
  n1791473b["dataform.midstream [table]"]
  n480b3019["dataform.upstream [table]"]
  n1dcc19cb["dataform_assertions.my_assertion"]
  n1791473b --> n1dcc19cb
  n480b3019 --> n1791473b
Loading

Testing

  • Tests: new unit tests in cli/mermaid_test.ts cover output, ordering, labels (including targets without a database, and graphs that only set the string table type), escaping, IDs (including a real 8-character hash collision) and the size warning's exact boundaries. New CLI tests cover filtering, flag conflicts and the warning (stderr only, exit code 0). scripts/lint and the full unit suite (scripts/run_tests) pass.
  • Real renders: rendered with the Mermaid CLI (Mermaid 11.17.2), including awkward names: dashes in project IDs, a table named end, Unicode and spaces, newlines and tabs, and labels containing #quot;, <br>, backticks, \n, fa:fa-user and $$x+1$$. Special characters displayed literally, and control characters as control pictures.
  • On GitHub (Mermaid 12.2.0): the final output for all of the awkward names above renders correctly in a secret gist. Diagrams over 50,000 characters were refused; with the real CLI, chains of up to 499 tables now stay under that limit.

🤖 Generated with Claude Code

@dmargol1
dmargol1 requested a review from a team as a code owner November 11, 2026 07:23
@dmargol1
dmargol1 requested review from apilaskowski and removed request for a team November 11, 2026 07:23
Comment thread cli/console.ts Resolved
Comment thread cli/console.ts Resolved
Comment thread cli/console.ts Resolved
@rafal-hawrylak

Copy link
Copy Markdown
Collaborator

/gcbrun

1 similar comment
@rafal-hawrylak

Copy link
Copy Markdown
Collaborator

/gcbrun

@apilaskowski

Copy link
Copy Markdown
Collaborator

/gcbrun

@apilaskowski
apilaskowski force-pushed the feat/compile-mermaid-output branch from 527dd50 to 22ce4b9 Compare November 23, 2026 01:53
@apilaskowski

Copy link
Copy Markdown
Collaborator

/gcbrun

@apilaskowski apilaskowski self-assigned this Nov 23, 2026
@apilaskowski
apilaskowski enabled auto-merge (squash) November 23, 2026 02:01
…graph as a Mermaid flowchart

Prints the same actions and dependencies as --dot as a Mermaid `graph LR`
flowchart, which GitHub, GitLab and other Mermaid-enabled tools render
natively. Works with the --output-* filters.

- Node IDs are `n` plus 8 hex characters of a SHA-256 hash of the full
  target (only colliding IDs are lengthened), keeping diagrams within
  renderer size limits.
- Labels show schema.name, plus the database when names are shared.
  Characters Mermaid would interpret are escaped; control characters are
  shown as Unicode control pictures.
- Output is sorted by target, so regenerated diagrams diff cleanly.
- Warns on stderr when the diagram exceeds Mermaid's default limits
  (500 edges, 50,000 characters).

Closes dataform-co#2434
…abel bugs

- Move mermaidRepresentation, mermaidSizeWarning, mermaidNodeIds and their
  helpers from cli/console.ts into cli/mermaid.ts, with their tests in
  cli/mermaid_test.ts.
- Don't prefix a database to a label when the target has no database (e.g. a
  project with no defaultDatabase whose action shares its schema.name with a
  declaration elsewhere), which rendered ".analytics.users" or
  "undefined.analytics.users".
- Derive the table type from the string `type` when `enumType` is unset, as
  prune() does, so graphs that only set the string type render
  "[table]" rather than "[unknown_type]" (or a TypeError) when compiled
  without --output-actions/--output-tags. Works on a copy, so the graph
  passed in is unchanged.
dotRepresentation had the same gap as mermaidRepresentation: graphs that
only set the string `type` render "[unknown_type]" (or throw a TypeError
when enumType is unset) when compiled without --output-actions or
--output-tags, because prune() is skipped.

- Move tableTypeLabel from cli/mermaid.ts to cli/util.ts and use it in
  both dotRepresentation and mermaidRepresentation.
- Add a dotRepresentation test in cli/console_test.ts for an in-memory
  graph with only `type` set and a protobuf round-tripped one, checking
  the input is unchanged.
@apilaskowski
apilaskowski force-pushed the feat/compile-mermaid-output branch from 22ce4b9 to 622b5db Compare November 23, 2026 02:03
@apilaskowski

Copy link
Copy Markdown
Collaborator

/gcbrun

@apilaskowski
apilaskowski merged commit 633a072 into dataform-co:main Nov 23, 2026
13 checks passed
apilaskowski pushed a commit that referenced this pull request Nov 23, 2026
Bump DF_VERSION from 3.1.6 to 3.1.7 to publish a new patch release of @dataform/core and @dataform/cli to npm. This release includes operations without hasOutput omitting dataset (#2476), domain project target and graph validation fixes (#2471), multiline rowConditions assertion fixes (#2424), BigQuery primitive field mappings (#2446), Mermaid graph output (#2435) and npm dependency security updates (#2472 and #2473). Publishing 3.1.7 allows projects to pin dataformCoreVersion to 3.1.7 in workflow_settings.yaml and install the updated CLI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: dataform compile --mermaid to output the dependency graph as a Mermaid flowchart

3 participants