Nested recurse query / Calculate level of nesting

What I want to do

I have a Task type, which can contain other tasks. So modelled in JSON, it would look like this for example:

{
  name: "Top Level task",
  tasks: [
    {
      name: "second level task",
      tasks: [
        {
          name: "Third level task",
          tasks: [
            {
              name: "fourth level task",
            }
          ]
        }
      ]
    },
    {
      name: "second level task",
    }
  ]
}

I would like to label each task with their level of nesting, producing a payload like this for example:

{
  name: "Top Level task",
  depth: 0,
  tasks: [
    {
      name: "second level task",
      depth: 1,
      tasks: [
        {
          name: "Third level task",
          depth: 2,
          tasks: [
            {
              name: "fourth level task",
              depth: 3,
            },
            {
              name: "fourth level task",
              depth: 3,
            }
          ]
        }
      ]
    },
    {
      name: "second level task",
      depth: 1,
    }
  ]
}

What I did

I was hoping to use the @recurse query by doing something like this:

tasks(func: uid(TOP_LEVEL_TASKS)) {
    name
    ~tasks @recurse {
      PARENTS_COUNT as count(uid)
      ~tasks
    }
    depth: sum(val(PARENTS_COUNT))
}

But that is supported at the time…
Is there another way to do this ?

1 Like