Specify number of hops(recursive query) when selecting graphs in surrealdb

37 views Asked by At

I'm trying to implement tree using surreal db. My tree looks like this. enter image description here

create node:root set title = 'Root';
create node:child1 set title = 'Child 1';
create node:child2 set title = 'Child 2';
create node:child3 set title = 'Child 3';
create node:grandchild11 set title = 'GrandChild 1.1'
relate node:root -> is_parent -> node:child1;
relate node:root -> is_parent -> node:child2;
relate node:root -> is_parent -> node:child3;
relate node:child1 -> is_parent -> node:grandchild11;

And now i want to select subtree starting from root recursively(eg being able to specify number of hops).

I tried this query and didn't quite understand how to archive recursiveness using this syntax.

select *,->is_parent->node as childen from node:root;

This query returns subtree but only for one level down.

So is there a way to specify number of hops or perform graph traversal recursively to get structure something like this.

{
  "title": "root',
  "children": [
    "title": "child1",
    "children": [
       {
         "title": "grandchild 1.1"
       }
    ],
    ...other root children nodes
  ]
}
0

There are 0 answers