Skip to main content

createGraph

Create a graph from a config. Resolves defaults for all fields.
export function createGraph<N = any, E = any, G = any>(
  config?: GraphConfig<N, E, G>,
): Graph<N, E, G>
config
GraphConfig<N, E, G>
Configuration for the graph. If omitted, creates an empty directed graph.
graph
Graph<N, E, G>
A fully-resolved graph object with all defaults applied.

Examples

const graph = createGraph();
// graph.id === ''
// graph.type === 'directed'
// graph.nodes === []
// graph.edges === []

createVisualGraph

Create a visual graph with required position/size on all nodes and edges.
export function createVisualGraph<N = any, E = any, G = any>(
  config?: VisualGraphConfig<N, E, G>,
): VisualGraph<N, E, G>
config
VisualGraphConfig<N, E, G>
Configuration for the visual graph. Extends GraphConfig with visual-specific properties.
graph
VisualGraph<N, E, G>
A visual graph with guaranteed position/size properties on all entities.

Examples

const graph = createVisualGraph({
  nodes: [{ id: 'a', x: 0, y: 0, width: 100, height: 50 }],
  edges: [{ id: 'e1', sourceId: 'a', targetId: 'a', x: 0, y: 0, width: 0, height: 0 }],
});
// graph.nodes[0].x === 0
// graph.direction === 'down'

GraphConfig

Input configuration type for creating graphs. All fields are optional and have sensible defaults.
export interface GraphConfig<
  TNodeData = any,
  TEdgeData = any,
  TGraphData = any,
> {
  id?: string;
  type?: 'directed' | 'undirected';
  initialNodeId?: string;
  nodes?: NodeConfig<TNodeData>[];
  edges?: EdgeConfig<TEdgeData>[];
  data?: TGraphData;
  direction?: 'up' | 'down' | 'left' | 'right';
  style?: Record<string, string | number>;
}

Type Parameters

TNodeData
any
Type of data attached to nodes. Defaults to any.
TEdgeData
any
Type of data attached to edges. Defaults to any.
TGraphData
any
Type of data attached to the graph. Defaults to any.

NodeConfig

Configuration type for creating or adding nodes.
export interface NodeConfig<TNodeData = any> {
  id: string;
  parentId?: string | null;
  initialNodeId?: string;
  label?: string;
  data?: TNodeData;
  x?: number;
  y?: number;
  width?: number;
  height?: number;
  shape?: string;
  color?: string;
  style?: Record<string, string | number>;
}
id
string
required
Unique node identifier. Must be a non-empty string.
parentId
string | null
ID of the parent node for hierarchical graphs. Set to null for top-level nodes.
initialNodeId
string
ID of the initial child node (for compound state nodes).
label
string
Display label for the node. Defaults to empty string.
data
TNodeData
Custom data attached to the node.
x
number
X coordinate for visual positioning.
y
number
Y coordinate for visual positioning.
width
number
Width of the node.
height
number
Height of the node.
shape
string
Visual shape identifier (e.g., ‘circle’, ‘rectangle’).
color
string
Color value for styling.
style
Record<string, string | number>
Custom style properties.

EdgeConfig

Configuration type for creating or adding edges.
export interface EdgeConfig<TEdgeData = any> {
  id: string;
  sourceId: string;
  targetId: string;
  label?: string;
  data?: TEdgeData;
  x?: number;
  y?: number;
  width?: number;
  height?: number;
  color?: string;
  style?: Record<string, string | number>;
}
id
string
required
Unique edge identifier. Must be a non-empty string.
sourceId
string
required
ID of the source node. Must be a non-empty string and reference an existing node.
targetId
string
required
ID of the target node. Must be a non-empty string and reference an existing node.
label
string
Display label for the edge. Defaults to empty string.
data
TEdgeData
Custom data attached to the edge.
x
number
X coordinate for visual positioning.
y
number
Y coordinate for visual positioning.
width
number
Width of the edge.
height
number
Height of the edge.
color
string
Color value for styling.
style
Record<string, string | number>
Custom style properties.

Build docs developers (and LLMs) love