OriginChain docs
examples · graph

Graph examples — copy-paste JSON.

← All examples

Twelve copy-paste JSON examples for the managed graph database. Direction-tagged relation keys mean forward and reverse adjacency are both single prefix scans — same WAL, same recovery, same backup as SQL and vector. See /docs/graph for the full reference.

Forward traversal

graph_neighbors — forward adjacency

Walk forward edges from a source node along a single relation. Direction-tagged keys make this a single prefix scan.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/neighbors?rel=follows&pk=alice" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "rel": "follows",
  "neighbors": ["bob", "carol", "dan"],
  "count": 3,
  "elapsed_ms": 2
}

graph_neighbors — with limit

Cap the fan-out. Useful for hot influencer-style nodes where the full neighbor set is large.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/neighbors?rel=follows&pk=alice&limit=10" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "rel": "follows",
  "neighbors": ["bob", "carol", "dan", "ed", "fay", "gita", "hari", "ira", "jay", "kim"],
  "count": 10,
  "elapsed_ms": 3
}

Reverse traversal

graph_reverse_neighbors — reverse adjacency

Walk reverse edges. Direction-tagged keys (REL_DIR_FWD / REV) make reverse a single prefix scan, not a full table walk.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/reverse?rel=follows&pk=bob" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "dst": "bob",
  "rel": "follows",
  "neighbors": ["alice", "ed", "fay"],
  "count": 3,
  "elapsed_ms": 2
}

BFS

graph_bfs — depth 1

BFS at depth 1 returns the same set as neighbors but tagged with depth. Useful when the consumer expects level annotation.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/bfs?rel=follows&pk=alice&max_depth=1" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "rel": "follows",
  "max_depth": 1,
  "frontier": [
    { "node": "bob",   "depth": 1 },
    { "node": "carol", "depth": 1 },
    { "node": "dan",   "depth": 1 }
  ],
  "elapsed_ms": 3
}

graph_bfs — depth-bounded multi-hop

Bounded BFS. Visits each node at most once. Honour max_depth to keep wide graphs from blowing up.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/bfs?rel=follows&pk=alice&max_depth=3" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "rel": "follows",
  "max_depth": 3,
  "frontier": [
    { "node": "bob",   "depth": 1 },
    { "node": "carol", "depth": 1 },
    { "node": "dan",   "depth": 1 },
    { "node": "ed",    "depth": 2 },
    { "node": "fay",   "depth": 2 },
    { "node": "gita",  "depth": 3 }
  ],
  "elapsed_ms": 7
}

Path + Dijkstra

graph_path — existence check between two nodes

Returns whether a directed path exists within the depth bound, plus the actual path if so.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/path?rel=follows&src=alice&dst=ed&max_depth=4" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "dst": "ed",
  "rel": "follows",
  "found": true,
  "depth": 2,
  "path": ["alice", "bob", "ed"],
  "elapsed_ms": 3
}

graph_path — no path within depth bound

Bounded reachability. found=false means no path within max_depth — not necessarily that no path exists at all.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/path?rel=follows&src=alice&dst=zara&max_depth=2" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "dst": "zara",
  "rel": "follows",
  "found": false,
  "elapsed_ms": 4
}

graph_dijkstra — weighted shortest path

Caller supplies edge weights as a JSON object. The engine runs Dijkstra over (src, dst, rel) and returns total cost plus path.

Request
curl "$ENGINE/v1/tenants/$T/graph/transit.stations/dijkstra?rel=line&src=central&dst=harbour&weights_json=%7B%22central-park%22%3A2%2C%22park-pier%22%3A4%2C%22pier-harbour%22%3A1%7D" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "central",
  "dst": "harbour",
  "rel": "line",
  "found": true,
  "cost": 7,
  "path": ["central", "park", "pier", "harbour"],
  "elapsed_ms": 5
}

graph_dijkstra — weights from a row field

When edges are real rows with a numeric weight column, point Dijkstra at that field and skip the inline weights_json.

Request
curl "$ENGINE/v1/tenants/$T/graph/transit.edges/dijkstra?rel=connects&src=central&dst=harbour&weight_field=minutes" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "central",
  "dst": "harbour",
  "rel": "connects",
  "weight_field": "minutes",
  "found": true,
  "cost": 14,
  "path": ["central", "park", "pier", "harbour"],
  "elapsed_ms": 6
}

Self-relations + multi-hop

Self-relation — edges within the same table

A relation can be defined from a table to itself, for example a follower graph. Source and destination both live in social.users.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/neighbors?rel=blocks&pk=alice" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "rel": "blocks",
  "neighbors": ["spammer-1", "spammer-2"],
  "count": 2,
  "elapsed_ms": 2
}

Multi-hop traversal — friends of friends

BFS at depth 2 surfaces second-degree neighbours. Combine with a follow-up SQL fetch on the resulting ids to materialise rows.

Request
curl "$ENGINE/v1/tenants/$T/graph/social.users/bfs?rel=follows&pk=alice&max_depth=2" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "alice",
  "rel": "follows",
  "max_depth": 2,
  "frontier": [
    { "node": "bob",   "depth": 1 },
    { "node": "carol", "depth": 1 },
    { "node": "ed",    "depth": 2 },
    { "node": "fay",   "depth": 2 }
  ],
  "elapsed_ms": 5
}

Cross-table relation — customer to order

Relations cross tables when both endpoints are declared in the manifest. Reverse adjacency answers "which customer placed this order?".

Request
curl "$ENGINE/v1/tenants/$T/graph/shop.customers/neighbors?rel=placed&pk=c_42" \
  -H "Authorization: Bearer $OC_TOKEN"
Response
{
  "src": "c_42",
  "rel": "placed",
  "neighbors": ["o_201", "o_205", "o_318"],
  "count": 3,
  "elapsed_ms": 3
}