> ## Documentation Index
> Fetch the complete documentation index at: https://docs.typesync.org/llms.txt
> Use this file to discover all available pages before exploring further.

# generate-graph

Generates a [Mermaid](https://mermaid.js.org) graph for the specified schema and injects it into the specified Markdown file. The generated graph is the visual representation of the database architecture inferred from your schema. You can specify where the graph is inserted within the file using the `--startMarker` and `--endMarker` options. For a detailed guide, see the full [example](#example) below.

## Usage

```bash theme={null}
typesync generate-graph --definition <filePathOrPattern> --outFile <filePath> --startMarker <startMarker> --endMarker <endMarker> --orientation <indentation> --debug <debug>
```

## Options

<ParamField type="string" path="definition" required>
  The exact path or a Glob pattern to the definition file or files. Each definition file must be a YAML or JSON file containing model definitions.

  * Example single file path: `definition/models.json`
  * Example Glob pattern: `'definition/**/*.yml'`
</ParamField>

<ParamField type="string" path="outFile" required>
  The path to the output file.
</ParamField>

<ParamField type="string" path="startMarker" default="typesync-start">
  A marker that indicates the line after which the generated code should be
  inserted. Make sure to use a string that is unique within the file. The line
  containing the marker must be commented i.e. the marker needs to appear after
  the `<!--` (see [example](#example)).
</ParamField>

<ParamField type="string" path="endMarker" default="typesync-end">
  A marker that indicates the line before which the generated code should be
  inserted. Make sure to use a string that is unique within the file. The line
  containing the marker must be commented i.e. the marker needs to appear after
  the `<!--` (see [example](#example)).
</ParamField>

<ParamField type="&#x22;vertical&#x22; | &#x22;horizontal&#x22;" path="orientation" default="horizontal">
  The orientation of the generated Mermaid graph. Can be either `"vertical"` or `"horizontal"` which correspond to the
  `"TB"` and `"LR"` Mermaid options, respectively.
</ParamField>

<ParamField type="boolean" path="debug" default={false}>
  Whether to enable debug logs.
</ParamField>

## Example

Suppose you have a schema definition file named `models.yml` and a Markdown file named `graph.md`.

<CodeGroup>
  ```yaml models.yml theme={null}
  # yaml-language-server: $schema=https://schema.typesync.org/v0.18.json

  Author:
    model: document
    path: authors/{authorId}
    type:
      type: object
      fields: {}

  Book:
    model: document
    path: books/{bookId}
    type:
      type: object
      fields: {}

  Review:
    model: document
    path: books/{bookId}/reviews/{reviewId}
    type:
      type: object
      fields: {}

  Chapter:
    model: document
    path: books/{bookId}/chapters/{chapterId}
    type:
      type: object
      fields: {}

  Translation:
    model: document
    path: books/{bookId}/translations/{translationId}
    type:
      type: object
      fields: {}
  ```

  ```md graph.md (initial) theme={null}
  # Architecture

  This graph explains how our database is structured.

  <!-- typesync-start -->
  <!-- typesync-end -->

  This graph is automatically generated.
  ```
</CodeGroup>

To generate a Mermaid graph for the defined models and inject them between the `typesync-start` and `typesync-end` markers in the `graph.md` file, you can run the following command:

```bash theme={null}
typesync generate-graph --definition definition.yml --outFile graph.md --startMarker typesync-start --endMarker typesync-end
```

Once you run the command, Typesync inserts the Mermaid graph definition into the specified section.

````md graph.md theme={null}
# Architecture

This graph explains how our database is structured.

<!-- typesync-start -->

```mermaid
graph LR
    node1["authors"] --> node2["{authorId}"]
    node3["books"] --> node4["{bookId}"]
    node4["{bookId}"] --> node5["chapters"]
    node5["chapters"] --> node6["{chapterId}"]
    node4["{bookId}"] --> node7["reviews"]
    node7["reviews"] --> node8["{reviewId}"]
    node4["{bookId}"] --> node9["translations"]
    node9["translations"] --> node10["{translationId}"]
``
<!-- typesync-end -->

This graph is automatically generated.
```
````

The graph generated for the above schema looks as follows:

```mermaid theme={null}
graph LR
    node1["authors"] --> node2["{authorId}"]
    node3["books"] --> node4["{bookId}"]
    node4["{bookId}"] --> node5["chapters"]
    node5["chapters"] --> node6["{chapterId}"]
    node4["{bookId}"] --> node7["reviews"]
    node7["reviews"] --> node8["{reviewId}"]
    node4["{bookId}"] --> node9["translations"]
    node9["translations"] --> node10["{translationId}"]
```
