Relationships¶
This guide covers querying graph relationships using nested field selections in GraphQL.
Nested Selections¶
In Grafeo, nested fields in a GraphQL query map to edge traversals. The field name corresponds to the edge type:
This query:
- Finds nodes with label
Personwherename = "Alice" - Returns the
nameproperty - Traverses outgoing
friendsedges - Returns
nameandageof connected nodes
How Nesting Maps to the Graph¶
| GraphQL | Graph Operation |
|---|---|
Root field name (Person) | Node label filter |
Scalar field (name) | Property access |
Object field (friends { ... }) | Edge traversal (outgoing) |
| Arguments on nested field | Filter on target nodes |
Multi-Level Nesting¶
Query multiple levels of relationships:
This traverses two hops: Alice's friends, then their friends.
Filtering Nested Results¶
Apply arguments to nested fields to filter related nodes:
Multiple Relationship Types¶
Query different relationship types in the same query:
Python Example¶
import grafeo
db = grafeo.GrafeoDB()
# Create a social graph
db.execute("INSERT (:Person {name: 'Alice', age: 30})")
db.execute("INSERT (:Person {name: 'Bob', age: 25})")
db.execute("INSERT (:Person {name: 'Charlie', age: 35})")
db.execute("""
MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
INSERT (a)-[:friends]->(b)
""")
db.execute("""
MATCH (b:Person {name: 'Bob'}), (c:Person {name: 'Charlie'})
INSERT (b)-[:friends]->(c)
""")
# Query relationships
result = db.execute_graphql("""
{
Person(name: "Alice") {
name
friends {
name
friends {
name
}
}
}
}
""")
for row in result:
print(row)
Rust Example¶
use grafeo_engine::GrafeoDB;
let db = GrafeoDB::new_in_memory();
// Create data
db.execute("INSERT (:Person {name: 'Alice'})").unwrap();
db.execute("INSERT (:Person {name: 'Bob'})").unwrap();
db.execute(
"MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'}) INSERT (a)-[:friends]->(b)"
).unwrap();
// Query with nested relationships
let result = db.execute_graphql(r#"
{
Person(name: "Alice") {
name
friends {
name
}
}
}
"#).unwrap();