Skip to main content
Functions for finding nodes directly connected by edges.

getNeighbors

Returns all neighbor nodes (successors + predecessors).
function getNeighbors<N>(
  graph: Graph<N>,
  nodeId: string
): GraphNode<N>[]
graph
Graph<N>
required
The graph to query
nodeId
string
required
ID of the node to get neighbors for
return
GraphNode<N>[]
Array of all neighboring nodes (both directions)
import { createGraph, getNeighbors } from '@statelyai/graph';

const graph = createGraph({
  nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
  edges: [
    { id: 'e1', sourceId: 'a', targetId: 'b' },
    { id: 'e2', sourceId: 'c', targetId: 'b' },
  ],
});

const neighbors = getNeighbors(graph, 'b');
// => [node a, node c]

getSuccessors

Returns direct successor nodes (targets of outgoing edges).
function getSuccessors<N>(
  graph: Graph<N>,
  nodeId: string
): GraphNode<N>[]
graph
Graph<N>
required
The graph to query
nodeId
string
required
ID of the node to get successors for
return
GraphNode<N>[]
Array of nodes that this node has outgoing edges to
import { createGraph, getSuccessors } from '@statelyai/graph';

const graph = createGraph({
  nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
  edges: [
    { id: 'e1', sourceId: 'a', targetId: 'b' },
    { id: 'e2', sourceId: 'a', targetId: 'c' },
  ],
});

const successors = getSuccessors(graph, 'a');
// => [node b, node c]

getPredecessors

Returns direct predecessor nodes (sources of incoming edges).
function getPredecessors<N>(
  graph: Graph<N>,
  nodeId: string
): GraphNode<N>[]
graph
Graph<N>
required
The graph to query
nodeId
string
required
ID of the node to get predecessors for
return
GraphNode<N>[]
Array of nodes that have outgoing edges to this node
import { createGraph, getPredecessors } from '@statelyai/graph';

const graph = createGraph({
  nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
  edges: [
    { id: 'e1', sourceId: 'a', targetId: 'c' },
    { id: 'e2', sourceId: 'b', targetId: 'c' },
  ],
});

const predecessors = getPredecessors(graph, 'c');
// => [node a, node b]

getEdgeBetween

Returns the edge from sourceId to targetId, or undefined if none exists. For undirected graphs, checks both directions.
function getEdgeBetween<E>(
  graph: Graph<any, E>,
  sourceId: string,
  targetId: string
): GraphEdge<E> | undefined
graph
Graph<any, E>
required
The graph to query
sourceId
string
required
ID of the source node
targetId
string
required
ID of the target node
return
GraphEdge<E> | undefined
The edge between the nodes, or undefined if none exists
import { createGraph, getEdgeBetween } from '@statelyai/graph';

const graph = createGraph({
  nodes: [{ id: 'a' }, { id: 'b' }],
  edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
});

const edge = getEdgeBetween(graph, 'a', 'b');
// => edge e1

const noEdge = getEdgeBetween(graph, 'b', 'a');
// => undefined (directed graph)

getSources

Returns all nodes with no incoming edges (inDegree = 0).
function getSources<N>(graph: Graph<N>): GraphNode<N>[]
graph
Graph<N>
required
The graph to query
return
GraphNode<N>[]
Array of all source nodes
import { createGraph, getSources } from '@statelyai/graph';

const graph = createGraph({
  nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
  edges: [
    { id: 'e1', sourceId: 'a', targetId: 'b' },
    { id: 'e2', sourceId: 'b', targetId: 'c' },
  ],
});

const sources = getSources(graph);
// => [node a]

getSinks

Returns all nodes with no outgoing edges (outDegree = 0).
function getSinks<N>(graph: Graph<N>): GraphNode<N>[]
graph
Graph<N>
required
The graph to query
return
GraphNode<N>[]
Array of all sink nodes
import { createGraph, getSinks } from '@statelyai/graph';

const graph = createGraph({
  nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
  edges: [
    { id: 'e1', sourceId: 'a', targetId: 'b' },
    { id: 'e2', sourceId: 'b', targetId: 'c' },
  ],
});

const sinks = getSinks(graph);
// => [node c]

Build docs developers (and LLMs) love