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() and iterate() 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

PageWhat it covers
Displaying Resultshow results are shown, the row and item limits
PredicatesP.* and TextP.*, comparison and resolution rules
Id Argumentswhat v(), e() and has_id() accept
Type Conversionas_string(), as_bool(), as_number(), length()
Math Expressionsthe math() step
Map Keys and Values, Non-String Keysmaps in the stream
Local Scopesteps applied inside a collection
Path Keys (jpath)reading and writing nested values
UUID Values, Multi-Label Vertices, Property Elementsvalues 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.