Edge Class
Represents a directed relationship between two nodes.
Import
import { Edge } from 'grafio';
Properties
id
readonly id: string
Unique identifier (auto-generated UUID).
sourceId
readonly sourceId: string
The source node ID (where the edge starts).
targetId
readonly targetId: string
The target node ID (where the edge ends).
type
readonly type: string
The edge type (e.g., 'KNOWS', 'CONTAINS', 'AUTHOR_OF').
properties
readonly properties: Readonly<Record<string, Primitive>>
The edge's key-value properties. Properties are deep-frozen for immutability.
Methods
toJSON()
toJSON(): EdgeData
Converts the edge to a JSON-serializable object.
Returns: EdgeData
interface EdgeData {
id: string;
sourceId: string;
targetId: string;
type: string;
properties: Record<string, Primitive>;
}
Example
const edge = await graph.addEdge(sourceId, targetId, 'KNOWS', { since: 2020 });
console.log(edge.id); // 'uuid-xxxx-xxxx'
console.log(edge.sourceId); // 'source-node-id'
console.log(edge.targetId); // 'target-node-id'
console.log(edge.type); // 'KNOWS'
console.log(edge.properties); // { since: 2020 }
// Serialize
const json = edge.toJSON();
/*
{
id: 'uuid-xxxx-xxxx',
sourceId: 'source-node-id',
targetId: 'target-node-id',
type: 'KNOWS',
properties: { since: 2020 }
}
*/
Immutability
Like Node properties, Edge properties are deep-frozen:
edge.properties.since = 2021; // TypeError: Cannot assign to read only property
Use Graph's edge property methods to modify:
await graph.updateEdgeProperty(edge.id, 'since', 2021);