cypher-dsl¶
Build Cypher queries as Python syntax objects and render them to Cypher text plus parameters. The library covers a read and write subset of Cypher 5, plus the Cypher 25 syntax it models. Supported Cypher lists exactly which cheat-sheet features it covers and which it does not. It does not connect to a database, so building a statement and executing it stay separate concerns.
from cypher_dsl import Configuration, Renderer, match, node, param
movie = node("Movie", name="m")
statement = (
match(movie)
.where(movie["released"] < param("year", 2000))
.returning(movie["title"])
)
renderer = Renderer.get(Configuration(server_version="5.26"))
rendered = renderer.render(statement)
assert rendered.cypher == (
"MATCH (m:Movie) WHERE m.released < $year RETURN m.title"
)
assert rendered.parameters == {"year": 2000}
Rendering does not change the statement, and every render returns its own parameter
dictionary: changing one result’s dictionary leaves the statement and other results alone.
To reuse one query with different request values, build the parameter with param(name)
and no value. The render reports it under unbound_parameters, and the code that runs
the query supplies the value. Request data goes through param() and never through
string interpolation.
Guide
- Getting started
- Supported Cypher
- Build graph patterns
- Build a node pattern
- Refer to a node without its label
- Test a label on a bound node
- Test an element during the match
- Add labels to an existing pattern
- Match labels with an expression
- Connect two patterns
- Extend a chain across relationships
- Quantify a relationship
- Quantify a path pattern
- Name a path
- Trace a path across segments
- Select which paths a
MATCHreturns - Reuse a bound pattern in a write
- What to watch out for
- Read data
- Match several patterns at once
- Keep rows that have no match
- Build conditions with DSL operators
- Test labels and types
- Test a value’s type
- Test whether a path exists
- Filter an UNWIND through WITH
- Call functions
- Project, alias and widen
- Deduplicate, order and paginate
- Group explicitly
- Compute values in the projection
- Choose between values with CASE
- Project selected properties into a map
- Iterate a list inside an expression
- Test the elements of a list
- Write data
- Compose statements
- Call subqueries
- Call procedures
- Bind parameters
- Choose a target
- Render a statement
Optional add-on