Render a statement

Rendering turns a statement into Cypher text and parameter containers. It never opens a connection: passing the result to a driver is the application’s job, and the library imposes nothing on how that happens.

Get a renderer for your target

Renderer.get validates one configuration and returns a renderer that holds no per-render state, so it can be stored in a module global and shared between threads:

from cypher_dsl import Configuration, Renderer, match, node

renderer = Renderer.get(Configuration(server_version="5.26"))

person = node("Person", name="p")
statement = match(person).returning(person["display_name"])

print(renderer.render(statement).cypher)
MATCH (p:Person) RETURN p.display_name

render accepts a Builder, a Statement, a UnionDraft or a UnionStatement. A Builder or a UnionDraft is finalised when it is rendered, and finalisation requires a complete query: the last clause must be RETURN, a write clause, or a CALL whose body does not return and which reports no status, and every name a variable reads must be bound at that position. A bare raw projection is the exception, because it marks the visible names unknown and the read is not checked. Use raw text in a projection explains that scope. The default configuration renders each statement on one line. Render statements for reading shows the multi-line layout. See Choose a target for how server_version and the language selection are chosen.

A finalised statement also renders itself: statement.render(configuration) returns the same Rendered as Renderer.get(configuration).render(statement). The statement’s parameters and unbound_parameters properties report what its build collected, without a render.

Read the render result

A Rendered carries the complete Cypher text, a fresh parameter dictionary and a frozenset of unbound parameter names:

from cypher_dsl import Configuration, Renderer, match, node, param

person = node("Person", name="p")
statement = (
    match(person)
    .where(person["email"] == param("email"))
    .returning(person["display_name"])
)

renderer = Renderer.get(Configuration(server_version="5.26"))
rendered = renderer.render(statement)

print(rendered.cypher)
print(rendered.parameters)
print(sorted(rendered.unbound_parameters))
print(rendered)
MATCH (p:Person) WHERE p.email = $email RETURN p.display_name
{}
['email']
MATCH (p:Person) WHERE p.email = $email RETURN p.display_name

Printing a result prints the Cypher text. Every render returns its own parameter dictionary, so changing one result’s dictionary leaves the statement and other results alone. See Bind parameters for how bindings are compared.

Supply the unbound values at execution

A render puts bound values in parameters and the remaining names in unbound_parameters, so the execution layer supplies the rest. Building the statement and completing the bindings needs no connection:

from cypher_dsl import Configuration, Renderer, match, node, param

renderer = Renderer.get(Configuration(server_version="5.26"))

person = node("Person", name="p")
statement = (
    match(person)
    .where(person["email"] == param("email"))
    .returning(person["display_name"])
)

rendered = renderer.render(statement)
parameters = {**rendered.parameters, "email": "ada@example.com"}

print(rendered.cypher)
print(parameters)
MATCH (p:Person) WHERE p.email = $email RETURN p.display_name
{'email': 'ada@example.com'}

With a running Neo4j instance:

from neo4j import GraphDatabase

with GraphDatabase.driver(
    "neo4j://localhost:7687", auth=("neo4j", "password")
) as driver:
    with driver.session(database="neo4j") as session:
        rows = session.run(rendered.cypher, parameters).data()

The driver is not a dependency of this library. Any client that accepts Cypher text plus a complete parameter mapping works: pass rendered.cypher and the complete bindings rather than the Rendered object.

Log the text, not the values

The rendered text contains placeholders instead of parameter values, so a value supplied only through param() cannot leak into a log. That assurance stops at parameters: lit() writes its value into the text, and raw() passes trusted text through unchanged, so either can expose a value. Keep request data in param() and keep secrets out of lit() and raw(). The values live in the parameter dictionary and stay under the caller’s control:

from cypher_dsl import Configuration, Renderer, match, node, param

person = node("Person", name="p")
statement = (
    match(person)
    .where(person["email"] == param("email", "ada@example.com"))
    .returning(person["display_name"])
)

renderer = Renderer.get(Configuration(server_version="5.26"))
rendered = renderer.render(statement)

print(f"slow query: {rendered.cypher}")
print(f"binding count: {len(rendered.parameters)}")
slow query: MATCH (p:Person) WHERE p.email = $email RETURN p.display_name
binding count: 1

Catch the library’s failures

DslError is the base of the library’s own exceptions, so one except clause covers them without catching unrelated Python exceptions:

from cypher_dsl import Configuration, Renderer, match, node
from cypher_dsl.errors import DslError

renderer = Renderer.get(Configuration(server_version="5.26"))

try:
    renderer.render(match(node("Person", name="p")))
except DslError as error:
    print(type(error).__name__)
    print(error)
DslError
MatchClause does not finalise a statement; add RETURN or a write clause.

The specific classes and when each is raised are listed in Catch the library’s own exceptions. A missing server_version or a DSL expression evaluated as a Python boolean raises TypeError, which is not a DslError.