Build your own knowledge graph database
From AST to rows: the query engine·14 min

Execute a single hop

0 / 1
# grade this stage (from your course directory)
npx theinference test --stage execute-single-hop --watch

Run a one-segment MATCH end to end: anchor, extend by one relationship, then project RETURN. Pick the anchor node pattern (the start node, which carries the label/property here), seed bindings via the indexes, then for the single segment call neighborEdges filtered by the segment's type and direction — keeping each extended binding whose neighbor also satisfies the target node pattern.

Your task

Implement matchPath + a minimal runQuery

Implement matchPath(store, path) for a one-segment pattern, then enough of runQuery to project RETURN m.title AS title. The fixture runs Kevin Bacon's ACTED_IN OUT movies — three titles.

src/executor.ts
// seed: candidates = findNodes(label, key, value) when label+prop present.
// extend: for each binding b, for each {rel, node} of neighborEdges(cur, type, dir),
//         if nodeMatches(node, targetPat) -> new binding {..b.vars, target: node}.
// project: resolve each ReturnItem (variable / variable.prop) to a value.
Process command
your_program.sh query {fixture}/fixtures/data/nodes.json {fixture}/fixtures/data/relationships.json "MATCH (bacon:Person {name:'Kevin Bacon'})-[:ACTED_IN]->(m:Movie) RETURN m.title AS title"
# -> [{"title":"A Few Good Men"},{"title":"Apollo 13"},{"title":"Frost/Nixon"}]
How it's graded

Process mode, stdout_json_match. No ORDER BY, so the rows are graded as a canonical set — sort them by their stable key before printing (the given cli.ts does this for you when there's no ORDER BY). All three movies must appear, no more.