# Walk the link graph

> Read the links between notes as a graph: neighbors, walks filtered by label and table, and the shortest path between two notes.

The engine reads your notes and their links as a labelled graph. Edges from link columns have the column’s name as their label, such as `cast` or `genre`; other resolved links have the label `link`, and each row has its table as its label.

The vault of the examples has three actors, two classic films, a viewing and two plain notes.

## See what a note links to and what links to it

`graph_neighbors` returns a note’s links in both directions. Each link has a label: the link column it comes from (`cast`), or `link` for a link in the text.

**`graph_neighbors`**

```json
{ "from_nodes": { "table": "actor", "where": { "name": "Cary Grant" } } }
```

Answer:

```json
{
  "nodes": [
    {
      "path": "datanotes/main/public/actor/Cary Grant.md",
      "table": "main.public.actor",
      "counts": { "in:cast": 2, "in:link": 1 },
      "edges": [
        {
          "path": "datanotes/main/public/film/Charade.md",
          "table": "main.public.film",
          "label": "cast",
          "direction": "in"
        },
        {
          "path": "datanotes/main/public/film/His Girl Friday.md",
          "table": "main.public.film",
          "label": "cast",
          "direction": "in"
        },
        { "path": "Notes/Film night.md", "label": "link", "direction": "in" }
      ]
    }
  ]
}
```

## Walk the links from a note

`graph_traverse` follows links breadth-first, in the `direction` you choose: `out`, `in` or `both`. Here it starts from Audrey Hepburn, goes up to two steps in both directions and keeps only films. It finds Charade, the film she’s in, and His Girl Friday, which the Charade note links to. `via` shows the route to each one.

**`graph_traverse`**

```json
{
  "from_nodes": { "paths": ["datanotes/main/public/actor/Audrey Hepburn.md"] },
  "direction": "both",
  "tables": ["film"],
  "max_depth": 2
}
```

Answer:

```json
{
  "start": ["datanotes/main/public/actor/Audrey Hepburn.md"],
  "nodes": [
    {
      "path": "datanotes/main/public/film/Charade.md",
      "table": "main.public.film",
      "depth": 1,
      "via": [
        {
          "from": "datanotes/main/public/actor/Audrey Hepburn.md",
          "label": "cast",
          "direction": "in",
          "to": "datanotes/main/public/film/Charade.md"
        }
      ]
    },
    {
      "path": "datanotes/main/public/film/His Girl Friday.md",
      "table": "main.public.film",
      "depth": 2,
      "via": [
        {
          "from": "datanotes/main/public/actor/Audrey Hepburn.md",
          "label": "cast",
          "direction": "in",
          "to": "datanotes/main/public/film/Charade.md"
        },
        {
          "from": "datanotes/main/public/film/Charade.md",
          "label": "link",
          "direction": "out",
          "to": "datanotes/main/public/film/His Girl Friday.md"
        }
      ]
    }
  ],
  "truncated": false
}
```

To read or total those rows, pass the paths to `select_rows` with `where: {"path": {"in": [...]}}`.

## Find how two notes are connected

`graph_path` finds the shortest chain of links between two notes, or between two sets of notes.

**`graph_path`**

```json
{
  "from_nodes": {
    "paths": ["Notes/Rewatch ideas.md", "datanotes/main/public/actor/Audrey Hepburn.md"]
  },
  "to_nodes": { "paths": ["Notes/Film night.md"] }
}
```

Answer:

```json
{
  "found": true,
  "length": 2,
  "via": [
    {
      "from": "datanotes/main/public/actor/Audrey Hepburn.md",
      "label": "cast",
      "direction": "in",
      "to": "datanotes/main/public/film/Charade.md"
    },
    {
      "from": "datanotes/main/public/film/Charade.md",
      "label": "link",
      "direction": "in",
      "to": "Notes/Film night.md"
    }
  ],
  "from": "datanotes/main/public/actor/Audrey Hepburn.md",
  "to": "Notes/Film night.md"
}
```
