Current Approach
I’m currently using the following DQL query to fetch a category and its subcategories recursively:
query categories($path: string) {
var(func: eq(Category.name, $path)) {
cat as uid
}
categories(func: uid(cat)) @recurse(depth: 10) {
uid
Category.name
Category.subcategories
}
}
With variables:
{
"$path": "Science"
}
This works well for a single category, but I want to extend it to handle arbitrary depths of categorisation.
Desired Functionality
I want to be able to query the subgraph starting from an arbitrary depth in the category hierarchy. For example, I’d like to be able to specify paths like:
["Science"]
"categories": [
{
"uid": "0xc392",
"Category.name": "Science",
"Category.subcategories": [
{
"uid": "0xc390",
"Category.name": "Physics",
"Category.subcategories": [
{
"uid": "0xc39b",
"Category.name": "Quantum Mechanics",
"Category.subcategories": [
...
["Science", "Physics"]
"categories": [
"uid": "0xc390",
"Category.name": "Physics",
"Category.subcategories": [
{
"uid": "0xc39b",
"Category.name": "Quantum Mechanics",
"Category.subcategories": [
...
["Science", "Physics", "Quantum"]
…
Or alternatively, using a path-like string:
"/Science/Physics/Quantum"
To determine the starting point
Questions
-
Is there a way to modify my current query to accept an array or path-like string of category names and traverse the graph from that point?
-
How can I efficiently query a subgraph starting from an arbitrary level of nesting without having to materialize the full path on each category node?
-
Are there any Dgraph-specific features or best practices for handling hierarchical data queries like this?
-
What would be the most performant way to implement this kind of flexible depth querying, especially for large category trees?
-
Are there any limitations or considerations I should be aware of when implementing this kind of query, particularly regarding query complexity or depth limits?
I’m aiming to create a flexible querying mechanism that allows me to start at any point in my category hierarchy and retrieve the subgraph from that point onwards. This should work efficiently regardless of the depth of the starting point in the overall category tree.
Any insights, sample queries, or best practices would be greatly appreciated. Thank you in advance for your help!
P.S.: Sub-graph query Possibly related but it hard-codes a specific nesting depth, and I need it to be dynamic so I can populate an UI.
EDIT: Implementing Filesystem like Graph System. How to fully get back tree without writing a ton of graphql code? is related but lacks an answer