Rust SDK and DSL
The Rust SDK gives HelixDB applications a typed way to describe graph reads, graph writes, and index work. Rust code builds a query request as a nested graph expression, sends it to a Helix server or an optional embedded database, and turns the response into an application value. The same SDK also loads a graph snapshot for local algorithms and exposes typed lifecycle results for long-running index work.
SDK boundary
The helix-db crate is the application-facing boundary between Rust and HelixDB. It combines a query language, request execution, response decoding, and optional in-process database access. The crate re-exports the main query types so application code can use one prelude rather than depend on the lower-level AST model directly. References: sdks/…/lib.rs• L3-17, sdks/…/dsl.rs• L1-19
Query request lifecycle
An application first builds a QueryRequest, then chooses how to execute it. A server client sends the request to the /v2/query route and decodes the response into the type requested by the caller. An embedded client runs the same request in process when the embedded feature is enabled. References: sdks/…/lib.rs• L378-487, sdks/…/lib.rs• L619-721
flowchart LR
A[Rust application] --> B[Query builder or query macro]
B --> C[QueryRequest]
C --> D{Client mode}
D -->|server| E[Helix /v2/query]
D -->|embedded| F[In-process HelixDB]
E --> G[Typed response or HelixError]
F --> G
The client keeps request concerns separate from query meaning. It can add server request policy such as writer routing, warm reads, or durability waiting before the query is sent. The response path distinguishes transport failure, server failure, invalid request data, and response conversion failure. References: sdks/…/lib.rs• L227-321, sdks/…/lib.rs• L543-617
Graph query DSL
The DSL is a builder language for composing graph work into a batch. A batch holds named query entries, and each entry holds a traversal that can start from nodes or edges, move through the graph, filter results, and end in a result operation. The batch type limits whether the traversal is read-only or can change data. References: sdks/…/lib.rs• L18-46, sdks/…/README.md• L7-38
Read and write batches
read_batch() describes read-only work. write_batch() describes work that can create, change, or remove graph data. Each .var_as(...) call gives an intermediate result a name, which lets later traversals use that result through a node or edge reference. .returning(...) selects the named results that form the response. References: sdks/…/README.md• L30-38, sdks/…/README.md• L40-57, sdks/…/README.md• L107-128
flowchart TD
A[read_batch or write_batch] --> B[Named traversal]
B --> C[Filter, navigate, aggregate, or mutate]
C --> D[Another named traversal]
D --> E[returning selected names]
E --> F[QueryRequest]
The DSL supports more than one style of graph work without changing this shape. Reads can find nodes or edges, follow links, filter properties, order and limit rows, project fields, aggregate values, search vectors or text, and preserve row bindings across branches. Writes can add nodes and edges, update properties, remove data, and manage indexes. References: sdks/…/README.md• L621-648, sdks/…/generate_parity_fixtures.rs• L624-989
Traversal composition
A traversal is a chain whose next step receives the result of the prior step. Source steps establish a node or edge stream. Navigation steps change the current graph element. Filter and terminal steps narrow or shape the result. Branch steps support optional paths, unions, choices, repetition, and grouped work while keeping the whole expression inside the batch request. References: sdks/…/README.md• L621-640, sdks/…/generate_parity_fixtures.rs• L757-839, sdks/…/generate_parity_fixtures.rs• L919-987
The DSL also models values and expressions as query data rather than as host-language substitutions. A literal is kept as a literal in the request. An Expr::param(...) value becomes a named request parameter, so the same query shape can run with different values. Predicates can compare properties with literals or expressions, and projections can return properties or computed expressions. References: sdks/…/README.md• L73-87, sdks/…/lib.rs• L1065-1093
Conditional and correlated results
Batch conditions control whether a later named query runs based on an earlier result. Row bindings solve a different problem: they keep related elements correlated within each traversal path. Together they let an application express conditional work and multi-hop result rows without rebuilding the relationship in Rust after the database returns. References: sdks/…/README.md• L89-105, sdks/…/README.md• L557-601
DSL construction and wire mapping
The Rust builder maps application intent to a nested abstract syntax tree (AST), which is the structured query form sent to HelixDB. The AST records batch kind, traversal order, references to named results, parameters, projections, and write or search operations. This keeps the database operation plan explicit while Rust provides type checks for valid combinations. References: sdks/…/dsl.rs• L1-19, sdks/…/generate_parity_fixtures.rs• L574-595
The mapping from Rust values to query values is recursive. Scalar values become typed query values. Arrays and string-keyed maps keep their shape. Date-time values become RFC 3339 strings, while byte values are rejected because the query JSON contract cannot represent them. The generated parameter schema travels with the values so the database can interpret them consistently. References: sdks/…/dsl.rs• L21-81, sdks/…/lib.rs• L45-158
Query serialization contract
The request contains the batch operation and may contain a query name, parameter values, and parameter types. Directly built requests have no query name. Macro-generated requests use the Rust function name as the query name. This name is metadata for the request; it does not turn the function into a server-side stored route. References: sdks/…/lib.rs• L603-616, sdks/…/lib.rs• L760-782, sdks/…/README.md• L282-289
Procedural query macros
The #[query] procedural macro turns a normal Rust function into a typed query factory. The function body remains the place where the graph operation is described, while the macro supplies the parameter expressions and builds a QueryRequest when the function is called. This gives application code a named Rust API for a reusable query shape. References: sdks/…/lib.rs• L348-474, sdks/…/main.rs• L1-23
Macro parameter model
The macro reads the function signature and supports a fixed set of query parameter shapes. It records each argument name as a parameter expression inside the query, then converts the call-time value into the matching query value. Nested arrays and string-keyed maps are supported. Invalid date-time values and byte parameters fail during request construction instead of reaching the database. References: sdks/…/lib.rs• L12-24, sdks/…/lib.rs• L250-345, sdks/…/dsl.rs• L26-80
The macro infers whether the query is a read or write from the batch constructor in the function body. A query function cannot be async or generic, and its arguments must use simple identifiers with supported types. These limits keep the generated function focused on request construction rather than execution. References: sdks/…/lib.rs• L168-190, sdks/…/lib.rs• L348-384
Graph snapshot API
The graph API turns one ordinary read batch into an immutable native graph. GraphSelection defines the node and edge streams, direction rules, retained properties, optional identity and edge-key fields, weights, and size limits. Client::graph sends that selection once, validates the returned rows, and gives graph algorithms a self-contained snapshot that does not query Helix again. References: sdks/…/graph.rs• L27-83, sdks/…/graph.rs• L226-325
The selection boundary protects the snapshot from ambiguous or unsafe input. Property names cannot be empty or use loader-owned names. Full node or edge scans require explicit opt-in. A size limit is checked as a completeness limit, so the loader rejects a result that exceeds the limit instead of returning a graph that looks complete but is truncated. References: sdks/…/graph.rs• L37-68, sdks/…/graph.rs• L109-177, sdks/…/graph.rs• L285-310, sdks/…/graph.rs• L488-519
The graph loader preserves the relationship between stored IDs and public graph identity. It also carries selected properties, edge keys, and weights into the local graph model. The result can then support storage-independent graph operations such as centrality, path work, communities, degrees, cycles, and layout. References: sdks/…/graph.rs• L328-360, sdks/…/graph.rs• L16-22, sdks/…/README.md• L650-674
Index lifecycle contracts
The lifecycle API gives Rust a typed view of asynchronous index work. A create or drop request can return an accepted operation, a reference to existing work, or an already-active result. Later status responses describe whether the operation is queued, running, blocked, succeeded, or aborted. References: sdks/…/lifecycle.rs• L20-51, sdks/…/lifecycle.rs• L233-272
The status model connects an operation to its logical index, physical generation, operation kind, index family, current stage, retry attempt, and monotonic progress counters. Blocked work includes a stable blocker code. This lets an application decide whether to wait, retry, abort, or report a source-data or configuration problem without parsing free-form text. References: sdks/…/lifecycle.rs• L53-72, sdks/…/lifecycle.rs• L75-156, sdks/…/lifecycle.rs• L192-230
The wire contract is strict for identity fields and known status values while allowing additive fields from a newer server. Numeric identifiers and progress counters use canonical decimal strings. An aborted result is valid only for build cleanup stages, which prevents an invalid server response from appearing as a normal terminal state in Rust. References: sdks/…/lifecycle.rs• L274-332, sdks/…/lifecycle.rs• L334-403
Examples and compatibility checks
The small example crate shows the intended application path: import the prelude, define a named query with #[query], call it with Rust values, and serialize the resulting request. It demonstrates query construction without adding a second application framework around the SDK. References: sdks/…/Cargo.toml• L1-10, sdks/…/main.rs• L1-23
The fixture generator provides a broad compatibility check for the Rust-to-wire mapping. It builds runtime and JSON-only requests, checks coverage of the public AST shapes, and can execute embedded fixtures for comparison. Its role is to detect drift between SDK request shapes and the operations expected by other clients or by the database runtime. References: sdks/…/generate_parity_fixtures.rs• L1-58, sdks/…/generate_parity_fixtures.rs• L279-571
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the AST or builder re-exports must stay aligned with the JSON fixtures and the server query contract.
- Changes to
#[query]parameter rules must update both generated request behavior and the declared parameter type/value mapping. - Changes to graph selection aliases or limits must stay aligned with graph response loading and reserved-property validation.
- Changes to lifecycle status names or stage rules must preserve decoding of additive server fields and validation of terminal states.
- Changes to server-only request options must keep embedded mode from accepting options it cannot apply.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A query function body contains neither read_batch() nor write_batch() | The macro cannot determine the request kind and compilation fails | Keep the batch constructor in the annotated function body |
A #[query] argument uses bytes or an invalid date-time value | Request construction returns a query error before execution | Use JSON-compatible values and valid date-time values |
| A graph selection starts with an all-node or all-edge source without opt-in | The graph request is rejected before it runs | Call allow_full_scan() only when the scan is deliberate |
| A graph response exceeds a configured node or edge limit | Loading fails instead of returning a partial snapshot | Treat limits as completeness bounds and size the selection accordingly |
| A server returns an invalid lifecycle identity or incompatible aborted status | Status decoding fails | Handle lifecycle responses through the typed SDK enums |
| An embedded client receives server-only request options | The request is rejected as invalid | Apply writer, warm, and durability options only to server clients |
Key files
| File | Why to read it |
|---|---|
| sdks/…/lib.rs | Shows the public client boundary, request execution flow, error model, and crate-level usage pattern. |
| sdks/…/dsl.rs | Shows the Rust-facing DSL exports, prelude, and parameter conversion boundary. |
| sdks/…/graph.rs | Shows how a read request becomes a validated local graph snapshot. |
| sdks/…/lifecycle.rs | Defines the typed contract for index operation receipts and status responses. |
| sdks/…/lib.rs | Explains how named Rust functions become parameterized query factories. |
| sdks/…/main.rs | Provides the smallest end-to-end example of DSL construction and serialization. |
| sdks/…/generate_parity_fixtures.rs | Shows the SDK's cross-language wire-shape and embedded-runtime compatibility check. |