Skip to content

Importing and exporting graphs · Mermaid

Export a graph when another part of your application needs its definition, or when someone needs to inspect its structure. Those are different uses of the same graph. A reusable definition must retain information such as conditions, languages and terminal outputs; a readable diagram can show a much smaller selection.

ExecKernel provides JSON, YAML and FlowDSL representations of the loaded definition. Mermaid adds a visual projection, with an optional rich mode that carries the definition alongside the drawing. Choose the representation according to what the recipient needs to do with it.

Importing constructs a Graph from a representation. Exporting serializes an existing Graph; it does not preserve the original file’s formatting or comments.

Representation PHP path into a graph Export path
JSON Decode to an array, then call SchemaImporter::importJson(). GraphSchemaExporter::export($graph, 'json'); CLI --format=json.
YAML Parse with Symfony Yaml, then pass the resulting array to SchemaImporter::importJson(). GraphSchemaExporter::export($graph, 'yaml'); CLI --format=yaml.
FlowDSL SchemaImporter::importDsl() accepts the DSL text. FlowDslExporter::export($graph) in PHP.
Rich Mermaid MermaidImporter::import() returns an array; ArrayGraphLoader constructs the graph from it. MermaidExporter with mode: 'rich'; CLI --format=mermaid-rich.
Visual Mermaid Intended for displaying the graph, without enough information to recover its definition. MermaidExporter with its default visual mode; CLI --format=mermaid.

The graph export command reads JSON, YAML and FlowDSL files. FlowDSL files must use .dsl or .flowdsl. The command does not offer a FlowDSL output format, and Mermaid documents enter through the PHP importer rather than as direct CLI input.

For the loading details, see JSON and YAML graph definitions and FlowDSL. MermaidImporter also supports ExecKernel’s annotated Mermaid subset, which supplies graph information such as languages and the root. That is a defined input format; an arbitrary Mermaid drawing is not automatically an executable graph. The rich path below is the way to recover a graph exported by ExecKernel.

We will reuse the learning-choice graph introduced in Anatomy of a graph. Its root, node 10, offers a guide through edge 101 and practice through edge 102. The practice edge requires practiceEnabled: true. The definition includes English and Spanish presentation records and outputs for the two terminals.

With learning-path.json in your project directory, these commands write two representations of the loaded graph:

Terminal window
vendor/bin/execkernel graph export learning-path.json --format=json --out=learning-path.exported.json
vendor/bin/execkernel graph export learning-path.json --format=yaml --out=learning-path.exported.yaml

--format selects the serializer; --out supplies the destination file. Both results retain the graph’s supported data, including the condition on edge 102 and both languages. The exporter writes its flat schema, so field spelling, defaults and layout can differ from the input even though the loaded definition is equivalent.

Your application can parse either file and import the resulting array. Converting it to YAML does not turn YAML into a native file loader: the YAML parser still supplies the data that the importer receives.

For a review of the available routes, export a visual projection:

Terminal window
vendor/bin/execkernel graph export learning-path.json --format=mermaid --lang=en --orientation=TD --out=learning-path.en.mmd

--lang=en selects the displayed titles and labels. --orientation=TD arranges the graph from top to bottom; LR is the alternative for a left-to-right layout. The exported source is:

flowchart TD
n10(["ROOT · 10 · How would you like to learn?"])
n20(("TERMINAL · 20 · Guide selected"))
n30(("TERMINAL · 30 · Practice selected"))
n10 -->|"E101 · Read the guide"| n20
n10 -->|"E102 · Practise in the browser [1 rule]"| n30

Mermaid rendering of root 10 with two branches: edge 101 leads to guide terminal 20, and edge 102 leads to practice terminal 30 and shows a count of one rule.

This figure renders the exported Mermaid source. The exporter marks the root and terminals, keeps their numeric IDs visible and includes the edge IDs in their labels. The [1 rule] annotation tells the reviewer that practice has a condition.

The picture does not show what that condition tests. It also leaves out the other language, descriptions, application data in extras, terminal outputs and webhook configuration. Titles and labels are simplified for safe display. A reviewer can understand the routes from this view, but your application cannot reconstruct the original definition from the drawing alone.

Use rich mode when the recipient needs both a readable view and a recoverable graph:

Terminal window
vendor/bin/execkernel graph export learning-path.json --format=mermaid-rich --lang=en --orientation=TD --out=learning-path.rich.mmd

The visible drawing is the same. The file also contains comments marked %% execkernel-rich:, %% execkernel-view: and %% execkernel-dsl:. These carry the document version, the selected view and an encoded FlowDSL definition. A Mermaid renderer ignores those comments; ExecKernel uses them to recover the graph.

Data in this example Visual file Rich file
Nodes, routes and IDs Shown in the drawing. Shown and retained in the embedded definition.
Condition on edge 102 Shows that there is one rule. Retains the practiceEnabled == true predicate.
English and Spanish presentation Shows the selected language. Retains all six metadata records and four labels.
Terminal outputs Omitted from the drawing. Retains all five output records.

Keep the full rich Mermaid file, including its comments. Copying only the visible flowchart lines removes the embedded definition.

MermaidImporter reads the document and returns definition data. ArrayGraphLoader then checks and constructs the Graph that your application can use. The importer requires an engine compatibility version; this example uses 1.0.0, also the built-in loader default.

In a PHP application with its Composer autoloader already loaded:

use ExecKernel\Infrastructure\Importer\MermaidImporter;
use ExecKernel\Infrastructure\Loader\ArrayGraphLoader;
$diagram = file_get_contents('learning-path.rich.mmd');
$importer = new MermaidImporter(engineVersion: '1.0.0');
$definition = $importer->import($diagram);
$graph = (new ArrayGraphLoader(
payload: $definition,
engineVersion: '1.0.0',
))->load('learning-path');

The final argument checks that the imported graph is the expected learning-path. The recovered graph contains the original IDs, both languages, the practice condition and the terminal outputs. Your application’s graph-handling code can now use it as it would a graph loaded from JSON.

To keep the recovered definition as a JSON file, pass that Graph to the schema exporter:

use ExecKernel\Infrastructure\Exporter\GraphSchemaExporter;
$json = (new GraphSchemaExporter())->export($graph, 'json');
file_put_contents('learning-path.recovered.json', $json);

The complete PHP example includes autoloading, file-read and file-write checks, and optional input and output filenames. Run it from your project directory if you want to perform the conversion as a command:

Terminal window
php import-rich-mermaid.php learning-path.rich.mmd learning-path.recovered.json
vendor/bin/execkernel validate learning-path.recovered.json --json

The recovered definition validates with three nodes, two edges, two terminals and no warnings. You can inspect the recovered JSON directly.

Rich import also checks that the visible projection agrees with its embedded definition. Editing only a node label or route in the Mermaid drawing causes a projection mismatch. Change the graph definition and export it again so the data and drawing remain consistent.

Webhook definitions can contain destinations, event subscriptions and secrets. Rich export requires an explicit choice when the graph contains them:

Terminal window
vendor/bin/execkernel graph export graph-with-webhooks.json --format=mermaid-rich --include-webhooks --out=graph-with-webhooks.rich.mmd

Here, graph-with-webhooks.json stands for your own definition containing webhooks. Without --include-webhooks, rich export fails rather than silently removing them. In PHP, the corresponding argument is includeWebhooks: true on MermaidExporter::export().

The embedded definition is encoded, not encrypted. JSON, YAML and FlowDSL exports also include stored webhook configuration; this Mermaid option is not a general redaction setting. Share a visual export when the recipient only needs the routes, and distribute a complete definition only where its configuration belongs.