feat: add Cypher query support and port missing Python sgraph methods#24
Merged
villelaitila merged 1 commit intosoftagram:mainfrom Mar 17, 2026
Merged
Conversation
This adds two major features to sgraph.js:
1. Cypher Query Interpreter (src/cypher/)
A lightweight, zero-dependency Cypher query engine for querying
SGraph models. Supports MATCH patterns with node labels and
relationship types, WHERE filtering (=, <>, CONTAINS, STARTS WITH,
ENDS WITH, AND, OR, NOT), RETURN with property access and aliases,
COUNT/COLLECT aggregation, type() function, ORDER BY, LIMIT, and
DISTINCT. Includes a CLI with table/json/csv output and interactive
REPL mode.
This mirrors the Python sgraph cypher.py module but uses a hand-
written parser+executor instead of the sPyCy dependency.
Usage:
import { cypherQuery } from 'sgraph.js';
const result = cypherQuery(model, 'MATCH (n:file) RETURN n.name');
CLI:
node dist/cypher/cli.js model.xml "MATCH (n:file) RETURN n.name"
2. Python sgraph Method Parity
Ports 22 methods from the Python sgraph library to close the API
gap between the two implementations:
SElement: getLevel, getRoot, isDescendantOf, getAncestorOfType,
getAncestorOfTypes, getAncestorOfLevel, getDescendants, getNodeCount,
getEACount, getEATypes, getEATypeCounts, getMaxDepth, hasType,
remove, rename, removeDescendantsIf
SElementAssociation: addAttribute
SGraph: getDepth, calculateModelStats
ModelApi: getElementByPath, filter, getChildrenByType
Bug fixes:
- SElementAssociation.remove(): fixed removing from fromElement.incoming
instead of toElement.incoming
- ModelApi.getUsedElements(): fixed mapping to ea.fromElement instead
of ea.toElement for outgoing dependencies
Also adds CLAUDE.md, PYTHON_MAPPING.md (bidirectional API mapping
between Python and TypeScript), and design/plan documentation.
Tests: 159 total (37 existing + 50 new method tests + 72 Cypher tests)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/cypher/) — lightweight, zero-dependency Cypher engine for querying SGraph models. Supports MATCH patterns, WHERE filtering, RETURN with aggregation (COUNT, COLLECT, type()), ORDER BY, LIMIT, DISTINCT. Includes CLI with table/json/csv output and interactive REPL.getLevel,getRoot,isDescendantOf,getAncestorOfType,getDescendants,getNodeCount,remove,rename,getDepth,calculateModelStats,filter,getChildrenByType, and more.SElementAssociation.remove()was removing from wrong element's incoming list;ModelApi.getUsedElementswas returning fromElement instead of toElement.Test plan
npm test)npm run compile && node dist/cypher/cli.js test/modelfile.xml "MATCH (n:file) RETURN n.name"remove()fix aligns with how frontend already uses it,getUsedElementsis unused by frontend)