Simple Remappings
Sometimes, you may want to rename a property that is returned from a traversal.
You can access properties of an item by using the name of the property as defined
in the schema.
::{new_name: property_name}
::{alias: _::{property}}
You can use the name directly, or if you want to be more explicit,
or in cases where there may be name clashes, you can use the _:: operator.
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 property remapping
QUERY GetUserWithAlias () =>
users <- N<User>::RANGE(0, 5)
RETURN users::{
userID: ::ID,
displayName: name
}
QUERY CreateUser (name: String, age: U8, email: String) =>
user <- AddN<User>({
name: name,
age: age,
email: email
})
RETURN user
See all 15 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 Johnson" , "age" : 25 , "email" : "alice@example.com" },
{ "name" : "Bob Smith" , "age" : 30 , "email" : "bob@example.com" },
{ "name" : "Charlie Brown" , "age" : 28 , "email" : "charlie@example.com" },
]
for user in users:
client.query( "CreateUser" , user)
result = client.query( "GetUserWithAlias" , {})
print ( "Users with remapped properties:" , result)
See all 15 lines
Nested Mappings
::|item_name|{
field: item_name::traversal,
nested_field: other::{
property: item_name::ID,
..
}
}
The important thing to note here, is that if we were to access the ID like we have shown previously:
nested_field: ID. This ID would be the ID of the other item, not the item_name item due to the tighter scope.
When accessing properties in nested mappings, scope matters. Use the explicit item_name::property syntax to access properties from outer scopes to avoid ambiguity.
Example 1: User posts with nested remappings
QUERY GetUserPosts (user_id: ID) =>
user <- N<User>(user_id)
posts <- user::Out<HasPost>
RETURN user::|usr|{
posts: posts::{
postID: ID,
creatorID: usr::ID,
creatorName: usr::name,
..
}
}
QUERY CreateUser (name: String, age: U8, email: String) =>
user <- AddN<User>({
name: name,
age: age,
email: email
})
RETURN user
QUERY CreatePost (user_id: ID, title: String, content: String) =>
user <- N<User>(user_id)
post <- AddN<Post>({
title: title,
content: content
})
AddE<HasPost>::From(user)::To(post)
RETURN post
See all 29 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 )
user_result = client.query( "CreateUser" , {
"name" : "Alice Johnson" ,
"age" : 25 ,
"email" : "alice@example.com"
})
user_id = user_result[ 0 ][ "user" ][ "id" ]
posts = [
{ "title" : "My First Post" , "content" : "This is my first blog post!" },
{ "title" : "Learning GraphDB" , "content" : "Graph databases are fascinating." },
{ "title" : "Weekend Plans" , "content" : "Planning to explore the city." },
]
for post in posts:
client.query( "CreatePost" , {
"user_id" : user_id,
"title" : post[ "title" ],
"content" : post[ "content" ]
})
result = client.query( "GetUserPosts" , { "user_id" : user_id})
print ( "User posts with nested remappings:" , result)
See all 26 lines