Skip to main content

Count elements with COUNT  

Count the number of elements in a traversal.
::COUNT 
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 
Here’s how to run the query using the SDKs or curl
from helix.client import Client  client = Client(local=True, port=6969)  users = [  {"name": "Alice", "age": 25, "email": "[email protected]"},  {"name": "Bob", "age": 30, "email": "[email protected]"},  {"name": "Charlie", "age": 28, "email": "[email protected]"},  {"name": "Diana", "age": 22, "email": "[email protected]"}, ]  for user in users:  client.query("CreateUser", user)  result = client.query("GetUserCount", {}) print(f"Total users: {result}") 

Scope results with RANGE  

Get a specific range of elements from a traversal.
::RANGE(start, end) 
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.

Example 1: Basic pagination

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 
Here’s how to run the query using the SDKs or 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) 

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 
Here’s how to run the query using the SDKs or curl
from helix.client import Client  client = Client(local=True, port=6969)  users = [  {"name": "Alice", "age": 25, "email": "[email protected]"},  {"name": "Bob", "age": 45, "email": "[email protected]"},  {"name": "Charlie", "age": 19, "email": "[email protected]"},  {"name": "Diana", "age": 32, "email": "[email protected]"},  {"name": "Eve", "age": 28, "email": "[email protected]"}, ]  for user in users:  client.query("CreateUser", user)  result = client.query("GetUsersOldestFirst", {}) print("Users (oldest first):", result) 

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 
Here’s how to run the query using the SDKs or curl
from helix.client import Client  client = Client(local=True, port=6969)  users = [  {"name": "Alice", "age": 25, "email": "[email protected]"},  {"name": "Bob", "age": 45, "email": "[email protected]"},  {"name": "Charlie", "age": 19, "email": "[email protected]"},  {"name": "Diana", "age": 32, "email": "[email protected]"},  {"name": "Eve", "age": 28, "email": "[email protected]"}, ]  for user in users:  client.query("CreateUser", user)  result = client.query("GetUsersYoungestFirst", {}) print("Users (youngest first):", result)