Writing Queries
This part describes the Gremlin DSL in detail, topic by topic. The DSL is the text form of a
query: what the command line, the playground, Python, saved queries and AI agents run, and what
script::eval_value evaluates in Rust. It is Gremlin embedded in Rhai.
The shape of a script
A script is a sequence of Rhai statements. g is the graph, __ starts an anonymous traversal,
and the token classes (P, T, Order, Scope, Column, Pop, ...) are predefined:
let min_age = 30;
let names = g.v().has_label("person").has("age", P.gt(min_age)).values("name").to_list();
names.len() // 2
- Terminals run a traversal.
to_list(),next()anditerate()run it and return native values; a traversal that is the last value of a script is displayed instead. A traversal in the middle of a script without a terminal is only built, not run. - Both spellings. Every step is available in snake_case (
has_label,out_e) and in Gremlin camelCase (hasLabel,outE), with the same overloads. - Strings in double quotes. Single quotes are Rhai character literals; maps are written
#{key: value}. - Anonymous traversals start with
__.:where(__.out("knows")),repeat(__.both()). - Every traversal is atomic. A failing traversal leaves nothing it wrote (Transactions).
An error names the failing step with its position in the plan, and most errors come with a help text that suggests a fix built from the failing query.
Topics
| Page | What it covers |
|---|---|
| Displaying Results | how results are shown, the row and item limits |
| Predicates | P.* and TextP.*, comparison and resolution rules |
| Id Arguments | what v(), e() and has_id() accept |
| Type Conversion | as_string(), as_bool(), as_number(), length() |
| Math Expressions | the math() step |
| Map Keys and Values, Non-String Keys | maps in the stream |
| Local Scope | steps applied inside a collection |
| Path Keys (jpath) | reading and writing nested values |
| UUID Values, Multi-Label Vertices, Property Elements | values and elements beyond strings and numbers |
The following chapters cover traversal patterns, changing the graph, schemas, saved queries and profiling; every single step is in the Step Reference.