# Read the paper graph (/docs/sdks/papers/graph)



The [Paper Graph](/docs/core-concepts/graph) is the structured content of a paper: an ordered list of nodes such as titles, paragraphs and tables.

## Load the graph [#load-the-graph]

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000");

    for (const node of graph.nodes) {
      console.log(node.position, node.type, node.page);
    }
    ```
  </Tab>
</Tabs>

## Load every node [#load-every-node]

Large papers are loaded one page at a time. Each call to `next()` adds the following page to `graph.nodes`, and `previous()` adds the page before it.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000");

    while (graph.cursor?.next) {
      await graph.next();
    }

    console.log(graph.nodes.length === graph.total);
    ```
  </Tab>
</Tabs>

## Filter nodes [#filter-nodes]

Only load nodes of some types, or from a single page of the document. Filters are kept when loading more nodes with `next()` and `previous()`.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000",
      /* [!code ++:4] */
      {
        type: ["table", "chart"],
        page: 3,
      }
    );
    ```
  </Tab>
</Tabs>

See the [List nodes](/docs/api/papers/graph/nodes/list) API reference for every type.

## Read a node [#read-a-node]

Each node includes:

| Field      | Description                                                                 |
| ---------- | --------------------------------------------------------------------------- |
| `id`       | The node's ID.                                                              |
| `type`     | The node's type, such as `paragraph_title`, `text` or `table`.              |
| `text`     | The plain text content of the node.                                         |
| `page`     | The page the node is on.                                                    |
| `position` | The node's position in the paper's reading order.                           |
| `bbox`     | Where the node is on the page.                                              |
| `refs`     | The text entries that make up the node, with their font, size and position. |
| `render`   | A signed URL to an image of the node's region.                              |
| `data`     | Data specific to the node's type, see [below](#node-data).                  |

## Node data [#node-data]

Tables, charts, images and titles are enriched with extra `data`. Use `is()` to check a node's type, which also gives you the right type for `data` in TypeScript.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    for (const node of graph.nodes) {
      if (node.is("table")) {
        console.log(node.data?.summary, node.data?.rows, node.data?.columns);
      }

      if (node.is("image", "header_image", "footer_image")) {
        console.log(node.data?.alt);
      }
    }
    ```
  </Tab>
</Tabs>

| Type                                    | Data                                                                                           |
| --------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `table`                                 | `summary`, `tags`, `rows`, `columns`, `cells`, `insights` and `importance`.                    |
| `chart`                                 | The chart `type`, `title`, `alt`, `summary`, `tags`, `source`, `insights` and `importance`.    |
| `image`, `header_image`, `footer_image` | The image `type`, `title`, `alt`, `tags`, detected `objects` and `entities`, and `importance`. |
| `doc_title`, `paragraph_title`          | The heading `level`.                                                                           |
| Any other type                          | Always `null`.                                                                                 |

`data` is `null` until the node is enriched.

## Get a node [#get-a-node]

Retrieve a single node by its ID, for example a node from a [search result](/docs/sdks/search/query). It doesn't need to be loaded in the graph yet.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000");

    const node = await graph.node("00000000-0000-0000-0000-000000000000");

    const context = await node.context();
    ```
  </Tab>
</Tabs>

A node retrieved this way isn't added to `graph.nodes`. Use its [context](/docs/sdks/papers/context) to read around it, since `next()` and `previous()` only work for nodes loaded in the graph.

## Move between nodes [#move-between-nodes]

Each node can reach its neighbours. Missing pages are loaded automatically. To load several neighbours at once, use the node's [context](/docs/sdks/papers/context).

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const node = graph.nodes[0];

    const next = await node.next();
    const previous = await node.previous(); // undefined at the start of the paper
    ```
  </Tab>
</Tabs>

## Options [#options]

<TypeTable
  type="{
  type: {
    description: 'Only load nodes of these types.',
    type: 'PaperNodeType[]',
    default: undefined,
  },
  page: {
    description: 'Only load nodes on this page of the document.',
    type: 'number',
    default: undefined,
  },
}"
/>
