Get first element of a list with FIRST
Get the first element from a traversal result.
Note this does not return the first/oldest node in the graph, but the first node that the traversal returns.
FIRST returns a single element (object) rather than a collection (array). If the traversal has no results, the binding will be empty.
Example 1: Getting the first node returned by a traversal
QUERY GetOneUser () =>
user <- N<User>::FIRST
RETURN user
See all 4 lines
Example 2: Getting the first node returned by an edge traversal
QUERY GetOneFollower (user_id: ID) =>
user <- N<User>(user_id)
first_follower <- user::In<Follows>::FIRST
RETURN first_follower
See all 5 lines
Count elements with COUNT
Count the number of elements in a traversal.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx file exactly.
Example 1: Basic element counting
QUERY GetUserCount () =>
user_count <- N<User>::COUNT
RETURN user_count
QUERY CreateUser (name: String, age: U8, email: String) =>
user <- AddN<User>({
name: name,
age: age,
email: email
})
RETURN user
See all 12 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 )
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "alice@example.com" },
{ "name" : "Bob" , "age" : 30 , "email" : "bob@example.com" },
{ "name" : "Charlie" , "age" : 28 , "email" : "charlie@example.com" },
{ "name" : "Diana" , "age" : 22 , "email" : "diana@example.com" },
]
for user in users:
client.query( "CreateUser" , user)
result = client.query( "GetUserCount" , {})
print ( f "Total users: { result } " )
See all 16 lines
Scope results with RANGE
Get a specific range of elements from a traversal.
RANGE is inclusive of the start but exclusive of the end. For example, RANGE(0, 10) returns elements 0 through 9 (10 total elements). Both start and end and required and must be positive integers.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx file exactly.
QUERY GetUsersPaginated (start: U32, end: U32) =>
users <- N<User>::RANGE(start, end)
RETURN users
QUERY CreateUser (name: String, age: U8, email: String) =>
user <- AddN<User>({
name: name,
age: age,
email: email
})
RETURN user
See all 12 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 )
for i in range ( 20 ):
client.query( "CreateUser" , {
"name" : f "User { i } " ,
"age" : 20 + (i % 40 ),
"email" : f "user { i } @example.com"
})
page1 = client.query( "GetUsersPaginated" , { "start" : 0 , "end" : 5 })
print ( "Page 1:" , page1)
page2 = client.query( "GetUsersPaginated" , { "start" : 5 , "end" : 10 })
print ( "Page 2:" , page2)
page3 = client.query( "GetUsersPaginated" , { "start" : 10 , "end" : 15 })
print ( "Page 3:" , page3)
See all 19 lines
Sort results with ORDER
Sort the results of a traversal by a property in ascending or descending order.
::ORDER<Asc>(_::{property})
::ORDER<Desc>(_::{property})
Use Asc for ascending order and Desc for descending order.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx file exactly.
Example 1: Sorting by age (oldest first)
QUERY GetUsersOldestFirst () =>
users <- N<User>::ORDER<Desc>(_::{age})
RETURN users
QUERY CreateUser (name: String, age: U8, email: String) =>
user <- AddN<User>({
name: name,
age: age,
email: email
})
RETURN user
See all 12 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 )
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "alice@example.com" },
{ "name" : "Bob" , "age" : 45 , "email" : "bob@example.com" },
{ "name" : "Charlie" , "age" : 19 , "email" : "charlie@example.com" },
{ "name" : "Diana" , "age" : 32 , "email" : "diana@example.com" },
{ "name" : "Eve" , "age" : 28 , "email" : "eve@example.com" },
]
for user in users:
client.query( "CreateUser" , user)
result = client.query( "GetUsersOldestFirst" , {})
print ( "Users (oldest first):" , result)
See all 17 lines
Example 2: Sorting by age (youngest first)
QUERY GetUsersYoungestFirst () =>
users <- N<User>::ORDER<Asc>(_::{age})
RETURN users
QUERY CreateUser (name: String, age: U8, email: String) =>
user <- AddN<User>({
name: name,
age: age,
email: email
})
RETURN user
See all 12 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 )
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "alice@example.com" },
{ "name" : "Bob" , "age" : 45 , "email" : "bob@example.com" },
{ "name" : "Charlie" , "age" : 19 , "email" : "charlie@example.com" },
{ "name" : "Diana" , "age" : 32 , "email" : "diana@example.com" },
{ "name" : "Eve" , "age" : 28 , "email" : "eve@example.com" },
]
for user in users:
client.query( "CreateUser" , user)
result = client.query( "GetUsersYoungestFirst" , {})
print ( "Users (youngest first):" , result)
See all 17 lines