mmasouras
(Michael)
January 23, 2019, 8:24pm
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!
mmasouras
(Michael)
January 23, 2019, 8:31pm
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"
}
]
},
MichelDiz
(Michel Diz)
January 23, 2019, 8:37pm
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_) } }
}
}
mmasouras
(Michael)
January 23, 2019, 8:50pm
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”.
MichelDiz
(Michel Diz)
January 23, 2019, 8:57pm
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 Get started with Dgraph
mmasouras
(Michael)
January 23, 2019, 9:06pm
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.
1 Like
What should I do if I want to expand all except the reverse edge?
thanks
selmeci
February 15, 2019, 4:15pm
8
I think expand(_forward_) can help you.
3 Likes
That’s what I’m looking for, really appreciate!