Iterate over collections with FOR loops Â
Use FOR loops to perform operations on each element in a collection. The loop variable can reference the entire element or specific properties.
FOR variable IN collection {
// Access element properties: variable.property
// Perform operations on each element
}
FOR loops process elements sequentially in the order they appear in the collection. This guarantees consistent execution order for dependent operations.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx file exactly.
QUERY UpdateUserStatus ( user_updates : [{ id : ID , status : String }]) =>
FOR { id , status } IN user_updates {
user <- N < User >( id ) :: UPDATE ({
status : status ,
last_updated : "2024-11-04"
})
}
RETURN "Updated all users"
QUERY CreateUser ( name : String , age : U8 , email : String , status : String ) =>
user <- AddN < User >({
name : name ,
age : age ,
email : email ,
status : status ,
last_updated : "2024-11-01"
})
RETURN user
See all 18 lines
Here’s how to run the query using the SDKs or curl
Python
Rust
Go
TypeScript
Curl
from helix.client import Client
client = Client( local = True , port = 6969 )
# Create initial users
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "[email protected] " , "status" : "active" },
{ "name" : "Bob" , "age" : 30 , "email" : "[email protected] " , "status" : "active" },
{ "name" : "Charlie" , "age" : 28 , "email" : "[email protected] " , "status" : "active" },
]
user_ids = []
for user in users:
result = client.query( "CreateUser" , user)
user_ids.append(result[ "id" ])
# Update their statuses using FOR loop
user_updates = [
{ "id" : user_ids[ 0 ], "status" : "inactive" },
{ "id" : user_ids[ 1 ], "status" : "pending" },
{ "id" : user_ids[ 2 ], "status" : "active" },
]
result = client.query( "UpdateUserStatus" , { "user_updates" : user_updates})
print ( "Update result:" , result)
See all 25 lines
Example 2: Processing query results
QUERY ArchiveInactiveUsers () =>
inactive_users <- N < User > :: WHERE ( _ :: { status } :: EQ ( "inactive" ))
FOR user IN inactive_users {
user :: UPDATE ({ archived : true , archived_at : "2024-11-04" })
}
RETURN "Archived inactive users"
QUERY CreateUser ( name : String , age : U8 , email : String , status : String ) =>
user <- AddN < User >({
name : name ,
age : age ,
email : email ,
status : status ,
archived : false ,
archived_at : ""
})
RETURN user
See all 17 lines
Here’s how to run the query using the SDKs or curl
Python
Rust
Go
TypeScript
Curl
from helix.client import Client
client = Client( local = True , port = 6969 )
# Create users with different statuses
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "[email protected] " , "status" : "inactive" },
{ "name" : "Bob" , "age" : 30 , "email" : "[email protected] " , "status" : "active" },
{ "name" : "Charlie" , "age" : 28 , "email" : "[email protected] " , "status" : "inactive" },
{ "name" : "Diana" , "age" : 22 , "email" : "[email protected] " , "status" : "active" },
]
for user in users:
client.query( "CreateUser" , user)
# Archive all inactive users
result = client.query( "ArchiveInactiveUsers" , {})
print ( "Archive result:" , result)
See all 18 lines
Example 3: Batch operations on collections
QUERY LoadProducts ( products : [{ name : String , price : F64 , category : String }]) =>
FOR product IN products {
new_product <- AddN < Product >({
name : product . name,
price : product . price,
category : product . category,
stock : 100
})
}
RETURN "Products loaded successfully"
See all 10 lines
Here’s how to run the query using the SDKs or curl
Python
Rust
Go
TypeScript
Curl
from helix.client import Client
client = Client( local = True , port = 6969 )
# Load multiple products in batch
products = [
{ "name" : "Laptop" , "price" : 999.99 , "category" : "Electronics" },
{ "name" : "Mouse" , "price" : 29.99 , "category" : "Electronics" },
{ "name" : "Keyboard" , "price" : 79.99 , "category" : "Electronics" },
{ "name" : "Monitor" , "price" : 299.99 , "category" : "Electronics" },
{ "name" : "Desk Chair" , "price" : 199.99 , "category" : "Furniture" },
]
result = client.query( "LoadProducts" , { "products" : products})
print ( "Load result:" , result)
See all 15 lines