Querying Graph
Advanced query patterns for navigating and exploring your graph.
Setup
import { InMemoryGraphFactory } from 'grafio';
import { CypherEngine } from 'grafio/cypher';
const factory = new InMemoryGraphFactory();
const graph = factory.forGraph('default');
const engine = new CypherEngine(graph);
Basic Queries
Match All Nodes
MATCH (n) RETURN n
const result = await engine.query('MATCH (n) RETURN n');
Match Nodes by Label
MATCH (p:Person) RETURN p.name, p.age
Match with WHERE Filter
MATCH (p:Person)
WHERE p.age > 25 AND p.city = 'NYC'
RETURN p.name, p.city
Follow Single Relationship
MATCH (a:Person)-[:KNOWS]->(b:Person)
RETURN a.name, b.name
Return with Aggregation
MATCH (p:Person)
RETURN COUNT(p) AS total, AVG(p.age) AS avgAge
Variable-Length Paths
Match paths of variable length between two nodes.
Fixed Range
// 1 to 3 hops
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person)
RETURN a.name, b.name
Exact Length
// Exactly 2 hops
MATCH (a:Person)-[:KNOWS*2]->(b:Person)
RETURN a.name, b.name
Unbounded
// Up to 5 hops
MATCH (a:Person)-[:KNOWS*..5]->(b:Person)
RETURN a.name, b.name
Zero or More
// Zero or more hops (includes self)
MATCH (a:Person)-[:KNOWS*]->(b:Person)
RETURN a.name, b.name
Named Paths
Capture traversal paths as variables for later use.
MATCH p = (a:Person)-[:KNOWS]->(b:Person)-[:KNOWS]->(c:Person)
WHERE length(p) > 1
RETURN p
Use Cases
- Finding paths between nodes
- Analyzing network structure
- Path validation
Pattern Combinations
Multiple Relationship Types
MATCH (p:Person)-[:KNOWS|FOLLOWS]->(other:Person)
RETURN p.name, other.name
Optional Type Matching
MATCH (p:Person|Product)-[r:KNOWS|BOUGHT]->(t:People|Product)
WHERE r.weight > 5
RETURN p.name, t.name
Aggregation with Paths
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person)
RETURN a.name, COUNT(b) AS reachableCount
ORDER BY reachableCount DESC
Finding Shortest Path
MATCH (start:Person {name: 'Alice'})-[*1..5]->(end:Person {name: 'Eve'})
RETURN end.name
Grafio uses BFS by default for shortest path queries.
DISTINCT with Variable Length
MATCH (a:Person)-[:KNOWS*1..2]->(b:Person)
RETURN DISTINCT a.name, b.name
Pagination
Combine with SKIP/LIMIT for paginated results:
MATCH (p:Person)-[:KNOWS]->(f:Person)
RETURN f.name
ORDER BY f.name
SKIP 0
LIMIT 10
Next Steps
- Cypher Language — basic syntax reference
- Filtering — WHERE clause patterns
- Aggregation — counting and grouping