# core/loaders/graph.loader.ts

> 22 lines of code and 6 definitions.

Tree: Bane's Lab Build Scripts
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/build-scripts#file-build-scripts-core-loaders-graph-loader-ts
Source text: https://banes-lab.com/source/build-scripts/core/loaders/graph.loader.ts.txt

Listed in [core/loaders](https://banes-lab.com/api/source/build-scripts/core/loaders.md), after [core/loaders/folder.loader.ts](https://banes-lab.com/source/build-scripts/core/loaders/folder.loader.ts.md) and before [core/loaders/heritage.loader.ts](https://banes-lab.com/source/build-scripts/core/loaders/heritage.loader.ts.md).

## Definitions

- `isGraph` (lexical_declaration, line 10)
- `readGraphReport` (lexical_declaration, line 14, exported)
- `REPORT_KEY` (lexical_declaration, line 8)
- `path` (lexical_declaration, line 15, exported)
- `report` (lexical_declaration, line 19, exported)
- `graph` (lexical_declaration, line 20, exported)

## Contained in

- [core/loaders](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-loaders.md)

## Uses

- [configuration/strings/graph.strings.ts](https://banes-lab.com/source/build-scripts/configuration/strings/graph.strings.ts.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-configuration-strings.md)
- [core/loaders](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-loaders.md)

## Source

```typescript
import { existsSync, readFileSync } from "node:fs";
import { graphReportEmpty, graphReportMissing } from "#configuration/strings/graph.strings";
import type { Graph } from "#types/graph.types";
import { graphReport } from "#core/resolvers/catalog.resolver";
import { isRecord } from "#core/selectors/base.selector";
import { relativePath } from "@ssot/paths";

const REPORT_KEY = "govlabHost.reports.content.graph";

const isGraph = function isGraph(value: unknown): value is Graph {
    return isRecord(value) && Array.isArray(value["nodes"]) && Array.isArray(value["edges"]);
};

export const readGraphReport = function readGraphReport(): { readonly graph: Graph; readonly report: unknown } {
    const path = graphReport();
    if (!existsSync(path)) {
        throw new Error(graphReportMissing(relativePath(REPORT_KEY)));
    }
    const report: unknown = JSON.parse(readFileSync(path, "utf8"));
    const graph = isRecord(report) ? report["graph"] : null;
    if (!isGraph(graph)) {
        throw new Error(graphReportEmpty(relativePath(REPORT_KEY)));
    }
    return { graph, report };
};
```
