Skip to main content

Basic Operations Examples

Minimal code examples for common graph operations.

Create a Graph

import { InMemoryGraphFactory } from 'grafio';

const factory = new InMemoryGraphFactory();
const graph = factory.forGraph('default');

Add Nodes

const alice = await graph.addNode('Person', { name: 'Alice' });
const bob = await graph.addNode('Person', { name: 'Bob' });

Connect Nodes

await graph.addEdge(alice.id, bob.id, 'KNOWS');

Query Nodes

// Get all nodes
const allNodes = await graph.getNodes();

// Get by type using filter options
const people = await graph.getNodes({ filter: { types: ['Person'] } });

// Get outgoing edges
const outgoingEdges = await graph.getEdgesFrom(alice.id);
const friends = outgoingEdges
.filter(e => e.type === 'KNOWS')
.map(e => e.targetId);

More Examples

See the Quick Start guide for a complete introductory example.