Is "expand" hiding empty nodes


(Michael) #1

I am learning dgraph and GraphQL± and I created a simple friends graph to experiment on.

I am issuing a query to get all the friends of a node, in two different ways.
The first way, when I explicitly name properties, returns all the friends of 0x1.
The second, which uses expand(all) omits 0x3 which only has incoming friends and no other edges.

Does expand ignore nodes that have no outgoing edges?

QUERY

{
  # get friends of 0x1, and their incoming friends.
  x1friends(func: uid(0x1)) {
	  friend {
	    uid
            name
            ~friend { uid}
    }
  }

  # get friends of 0x1, and expand the results
  x1friendsexpanded(func: uid(0x1)) {
    friend {
	expand(_all_)
    }
  }
}

RESPONSE

{
  "data": {
    "x1friends": [
      {
        "friend": [
          {
            "uid": "0x2",
            "name": "Mehdi",
            "~friend": [
              {
                "uid": "0x1"
              }
            ]
          },
          {
            "uid": "0x3",
            "~friend": [
              {
                "uid": "0x1"
              },
              {
                "uid": "0x2"
              }
            ]
          }
        ],
        "uid": "0x1"
      }
    ],
    "x1friendsexpanded": [
      {
        "friend": [
          {
            "name": "Mehdi",
            "uid": "0x2"
          }
        ],
        "uid": "0x1"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 18195,
      "processing_ns": 2506983,
      "encoding_ns": 768663
    },
    "txn": {
      "start_ts": 2108
    }
  }
}

Thanks!


(Michael) #2

Actually, 0x3 also has a friend, but their friend (0x4) has no edges:

{  
 x3friends(func: uid(0x3)) {
    friend {
	uid
    }
  }
}
 "data": {
    "x3friends": [
      {
        "friend": [
          {
            "uid": "0x4"
          }
        ],
        "uid": "0x3"
      }
    ]
  },


(Michel Conrado) #3

Just add more levels to expand:

e.g

  # get friends of 0x1, and expand the results
  x1friendsexpanded(func: uid(0x1)) {
    friend {
	expand(_all_) { expand(_all_) }
    }
  }

or

  # get friends of 0x1, and expand the results
  x1friendsexpanded(func: uid(0x1)) {
    friend {
	expand(_all_) { expand(_all_) { expand(_all_) } }
    }
  }

(Michael) #4

Thank you, that works for the current data but it seems it will not work if the other node is empty too.

I created another predicate called “relative” that does not have a reverse.

{
  x1relatives(func: uid(0x1)) {
   relative {
		uid
    }
  }
  
  x1relativesexpand(func: uid(0x1)) {
    relative {
      expand(_all_) {
        expand(_all_)
      }
    }
  }
}
  "data": {
    "x1relatives": [
      {
        "relative": [
          {
            "uid": "0xf"
          },
          {
            "uid": "0x10"
          }
        ],
        "uid": "0x1"
      }
    ],
    "x1relativesexpand": []
  }

Can you clarify the semantics of “expand”.


(Michel Conrado) #5

Ok, now for empty nodes you just need to add “uid” among the expand func.

e.g:

x1relativesexpand(func: uid(0x1)) {
    relative {
      uid
      expand(_all_) {
        uid
        expand(_all_)
      }
    }
  }

The expand func will actually expand only when there are predicates with values. Otherwise it will return empty. So the trick here is to use “uid”.

Not sure how I can be more deep here, but all about it you can find on docs https://docs.dgraph.io/query-language#expand-predicates


(Michael) #6

That’s a neat trick - thanks!

I think if you add these caveats for empty nodes and the uid trick it would help - that’s what I mean by “semantics” - i.e. how does it work exactly.


(Thewisewolf Horo) #7

What should I do if I want to expand all except the reverse edge?:sweat_smile:

thanks


#8

I think expand(_forward_) can help you.


(Thewisewolf Horo) #9

That’s what I’m looking for, really appreciate!:kissing_heart: