Modeling a tree and getting a subtree from a node

Hey guys, I don’t thing we have a bug here. The recurse directive usage that is wrong.

For this case the usage would be:

{
   me(func: has(jungle)) @recurse(depth: 10, loop: true) {
    uid
    name
    jungle 
    animals
    birds
    lion
    elephant
    parrot
    chicken
  }
}

Response

{
  "data": {
    "me": [
      {
        "uid": "0x5",
        "jungle": [
          {
            "uid": "0x6",
            "animals": [
              {
                "uid": "0x7",
                "lion": [
                  {
                    "uid": "0x8",
                    "name": "Simba"
                  }
                ],
                "elephant": [
                  {
                    "uid": "0x1",
                    "name": "Ele"
                  }
                ]
              }
            ],
            "birds": [
              {
                "uid": "0x2",
                "parrot": [
                  {
                    "uid": "0x3",
                    "name": "Zuzu"
                  }
                ],
                "chicken": [
                  {
                    "uid": "0x4",
                    "name": "Chica"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

Update

Just for fun you can create one (or more) Node (s) as templates to use in recurse directive. And then expand the predicates like this:

{
  "set": [
    {
      	"model": "root", 
        "animals": {
          "uid": "0x1" #0x1 is dummy node for us
        },
        "name": " ",
        "lion": {
          "uid": "0x1"
        },
        "elephant": {
          "uid": "0x1"
        },
        "birds": {
          "uid": "0x1"
        },
        "parrot": {
          "uid": "0x1"
        },
        "chicken": {
          "uid": "0x1"
        }
    }
  ]
}

Query

{
  var(func: has(model)) {
     pred as _predicate_
  }
  parent as var(func: has(animals)) @filter(NOT has(model) ) 

  me(func: uid(parent)) @recurse(depth: 10, loop: true) {
    expand(val(pred)) {
      expand(_all_)
    }
  }
}

Response

{
  "data": {
    "me": [
      {
        "animals": [
          {
            "lion": [
              {
                "name": "Simba"
              }
            ],
            "elephant": [
              {
                "name": "Ele"
              }
            ]
          }
        ],
        "birds": [
          {
            "parrot": [
              {
                "name": "Zuzu"
              }
            ],
            "chicken": [
              {
                "name": "Chica"
              }
            ]
          }
        ]
      }
    ]
  }
1 Like