Skip to main content
Guide
Mutations require a write batch. A mutation in a read batch fails at compile time in Rust and TypeScript, with ErrWriteTraversalInReadBatch at validation or serialization in Go, and with TypeError when the entry is added in Python. The server rejects it too.

Create two nodes and an edge

How addN and addE work

  • addN at the start of a traversal creates one node. After a stream, it creates one node per input row.
  • addE(label, to, properties) creates an edge from every current node to every target (two sources and three targets make six edges) and returns the new edges, so count() counts edges. Rust, TypeScript, and Python type the result as a node stream even though it holds edges.
  • Edges are never deduplicated: running the same addE twice creates parallel edges with different IDs.
  • An empty source stream creates nothing. Otherwise, a target reference that resolves to no nodes fails with invalid_query, and a missing target ID fails with node_not_found.

Other mutations

Insert many rows

forEachParam / for_each_param runs a batch body once per object in an array parameter. Inside the body, each field of the current row is a parameter: PropertyInput.param("email") reads that row’s email. Declare each row’s fields so every value gets its type. Here joinedAt is a date_time field, so it is stored as a datetime that range filters and range indexes match, not as RFC3339 text. See Typed object fields.
  • The parameter must be an array of objects. Any other value fails type validation before the query runs when typed as here (invalid_query_json over HTTP), or with invalid_query when untyped. An empty array runs the body zero times.
  • Every row must have the declared fields, except nullable ones, which a row may omit. A missing field that is not nullable, an undeclared field, a wrong-typed value, an invalid RFC3339 string in a date_time field, or null in any field not declared value or nullable rejects the whole request before anything is written. Declare a column that can be null, such as a deletedAt timestamp, as param.nullable(param.dateTime()) ({ "nullable": "date_time" }); see Nullable fields. A row that omits a field the body reads, such as PropertyInput.param("deletedAt"), fails as an unbound parameter, even when another parameter or an enclosing row has a field of that name, so send null for a column the body writes.
  • Rows declared as a bare { "array": "object" } (param.object(), QueryParamType::Object, ParamTypeObject()) are not checked field by field, and their RFC3339 strings stay strings.
  • Variables are not per-iteration: after the loop, user holds only the last row’s result, so this request returns nothing.
  • All rows commit in one transaction. Split very large imports into several requests, each within the index backpressure limits when the label has vector or text indexes.

Create or update a node

To upsert, read the node by its key, then add two conditional entries: one creates the node if the read is empty, the other updates it otherwise.
Exactly one of created and updated runs; the other returns []. Back the key with a unique index (nodeUniqueEquality("User", "email")): the lookup becomes an index read, and only one of two concurrent upserts of the same key creates the node. The other fails with unique_constraint_violation or a retryable transaction_conflict; its retry takes the update branch.

Transaction behavior

A write batch of graph mutations is one transaction: if any entry fails, nothing commits. Reads inside the batch can refer to earlier mutations through named variables. Do not retry a write automatically unless the full request is safe to replay. Index operations differ:
  • A request cannot mix graph mutations with createIndexIfNotExists, dropIndex, retryIndexOperation, or abortIndexOperation; it fails before anything commits. getIndexOperation can share a request with graph writes.
  • Several index operations in one request commit separately: a failure leaves the earlier ones committed.
Cypher has no batch type: the server detects that a statement writes and runs it in one transaction, so the Cypher tab’s three CREATE clauses also commit or roll back together. SQL sends one statement per request, so the SQL tab is two requests and two transactions: the first commits both users, and the second connects them.

Next steps

Typed parameters

Pass request-specific values without changing the AST shape.

Secondary indexes

Accelerate equality, uniqueness, and range lookups.