UpsertN differs from AddN in that it will update an existing node if one is found in the traversal context, rather than always creating a new one. If no existing node is found, it creates a new node just like AddN.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx file exactly.
QUERY UpsertPerson(name: String, age: U32) => person <- UpsertN<Person>({name: name, age: age}) RETURN person
Here’s how to run the query using the SDKs or curl
Copy
Ask AI
from helix.client import Clientclient = Client(local=True, port=6969)# First call creates a new personperson = client.query("UpsertPerson", { "name": "Alice", "age": 25,})print("Created person:", person)# Subsequent calls with same data update the existing personupdated = client.query("UpsertPerson", { "name": "Alice", "age": 26,})print("Updated person:", updated)
When used after a traversal that returns existing nodes, UpsertN will update those nodes rather than creating new ones.
Copy
Ask AI
QUERY UpdateOrCreatePerson(name: String, new_age: U32) => // Find existing person or create new one with the given properties existing <- N<Person>::WHERE(_::{name}::EQ(name)) person <- existing::UpsertN<Person>({name: name, age: new_age}) RETURN person
Here’s how to run the query using the SDKs or curl
Copy
Ask AI
from helix.client import Clientclient = Client(local=True, port=6969)# If "Alice" exists, updates her age; otherwise creates a new personresult = client.query("UpdateOrCreatePerson", { "name": "Alice", "new_age": 30,})print("Upserted person:", result)