Skip to main content

Graph Class

The main entry point for working with Grafio graphs.

Import

import { Graph } from 'grafio';

Constructor

new Graph(storageProvider?: IStorageProvider)

Creates a new Graph instance. If no storage provider is provided, InMemoryStorageProvider is used by default.

Parameters:

NameTypeDefaultDescription
storageProviderIStorageProviderInMemoryStorageProviderThe storage backend

Node Operations

addNode()

async addNode(
type: string,
properties?: Record<string, unknown>,
transaction?: GraphTransaction
): Promise<Node>

Adds a new node to the graph.

getNode()

async getNode(id: string, transaction?: GraphTransaction): Promise<Node | undefined>

hasNode()

async hasNode(id: string, transaction?: GraphTransaction): Promise<boolean>

getNodes()

async getNodes(options?: GraphQueryOptions): Promise<readonly Node[]>

removeNode()

async removeNode(
id: string,
cascade?: boolean,
transaction?: GraphTransaction
): Promise<boolean>

Edge Operations

addEdge()

async addEdge(
sourceId: string,
targetId: string,
type: string,
properties?: Record<string, unknown>,
transaction?: GraphTransaction
): Promise<Edge>

getEdge()

async getEdge(id: string, transaction?: GraphTransaction): Promise<Edge | undefined>

hasEdge()

async hasEdge(id: string, transaction?: GraphTransaction): Promise<boolean>

getEdges()

async getEdges(options?: GraphQueryOptions): Promise<readonly Edge[]>

removeEdge()

async removeEdge(id: string, transaction?: GraphTransaction): Promise<boolean>

getEdgesFrom()

async getEdgesFrom(sourceId: string, options?: GraphQueryOptions): Promise<Edge[]>

Gets all outgoing edges from a node.

getEdgesTo()

async getEdgesTo(targetId: string, options?: GraphQueryOptions): Promise<Edge[]>

Gets all incoming edges to a node.

getDirectEdgesBetween()

async getDirectEdgesBetween(
sourceId: string,
targetId: string,
options?: GraphQueryOptions
): Promise<Edge[]>

Gets all edges between two nodes (in either direction).

Traversal & Analysis

traverse()

async traverse(
sourceId: string | string[],
targetId: string | string[],
options?: TraversalOptions
): Promise<string[][] | null>

isDAG()

async isDAG(): Promise<boolean>

topologicalSort()

async topologicalSort(): Promise<string[] | null>

warmCache()

async warmCache(): Promise<void>

Serialization

exportJSON()

async exportJSON(): Promise<GraphData>

importJSON()

static async importJSON(data: GraphData, storageProvider?: IStorageProvider): Promise<Graph>

Property Operations

addNodeProperty()

async addNodeProperty(
nodeId: string,
key: string,
value: unknown,
transaction?: GraphTransaction
): Promise<void>

updateNodeProperty()

async updateNodeProperty(
nodeId: string,
key: string,
value: unknown,
transaction?: GraphTransaction
): Promise<void>

deleteNodeProperty()

async deleteNodeProperty(
nodeId: string,
key: string,
transaction?: GraphTransaction
): Promise<void>

clearNodeProperties()

async clearNodeProperties(nodeId: string, transaction?: GraphTransaction): Promise<void>

addEdgeProperty()

async addEdgeProperty(
edgeId: string,
key: string,
value: unknown,
transaction?: GraphTransaction
): Promise<void>

updateEdgeProperty()

async updateEdgeProperty(
edgeId: string,
key: string,
value: unknown,
transaction?: GraphTransaction
): Promise<void>

deleteEdgeProperty()

async deleteEdgeProperty(
edgeId: string,
key: string,
transaction?: GraphTransaction
): Promise<void>

clearEdgeProperties()

async clearEdgeProperties(edgeId: string, transaction?: GraphTransaction): Promise<void>

Admin Operations

createIndex()

async createIndex(entityType: 'node' | 'edge', propertyKey: string): Promise<void>

clear()

async clear(): Promise<void>

Transactions

createTransaction()

createTransaction(): GraphTransaction

supportsTransactions()

supportsTransactions(): boolean

GraphQueryOptions

interface GraphQueryOptions {
filter?: {
nodeType?: string;
edgeType?: string;
};
orderBy?: IOrderBy[];
limit?: number;
offset?: number;
transaction?: GraphTransaction;
}