Search by edge uid

This is probably a very nooby question, but imagine I have a bill, whose uid I know, and a bunch of billItem nodes (that include name and price) that point towards that bill through the edge “bill”. How can I search specifically for all those billItems whose “bill” edge points to that uid?

Here’s what I’ve tried:

{
  search(func: uid(bill, 0xa)) {
    uid
  }
}

Error received:

Some variables are used but not defined Defined:[] Used:[bill]

Ok, so it thinks bill is a variable, understood!

Then I tried:

{
  search(func: eq(bill, 0xa)) {
    uid
  }
}

And got error:

Attribute bill is not valid scalar type while running: name:"eq" args:"0xa"

Well yeah, I know it’s not a scalar type, it’s a uid!

Then I discovered uid_in! Surely this is the answer I’m looking for!

{
  search(func: uid_in(bill, 0xa)) {
    uid
  }
}

But

uid_in function not allowed at root

Why not? So either I’m completely missing a really simple function, or I have to do it this way:

{
  search(func: uid(0xa)) {
    billItems {
      uid
    }
  }
}

Which is fine, but is there no way to access those billItems directly based on the uid of their “bill” edge?

Michael, You’re missing a lot of concepts here. I think the Tour might help you http://tour.dgraph.io

All piece of text inserted within the parameters of a func will be considered as variable.

The concept here is that you are mixing eq with uid functions. They are different things. You will never find the UID of a Bill within the predicate Bill. For this is in the “predicate” UID.

The uid_in proposal is to locate a relation ahead. So you need a reference, so in ROOT it’s forbidden. Because you first use the uid function to later use uid_in. But such use would be useless, too. Or just used to “check”.

The concept of uid_in is “Check if this node is related to this other node”. Use it in Root is illogical, as it does not have the “this” referential.

Maybe this query below will help.

(Please note that in this case you will need to billItems as “@Reverse”)

Whereas “0xa” belongs to a Parent Node.

{
  search(func: has(billItems)) @filter(uid_in(~billItems, "0xa" ))  
    {
    uid
    expand(_all_){ expand(_all_) }
  }
}
2 Likes

Michel to the rescue again! Thanks

Believe you me, nobody is more aware of that than me! And I’ve done the tour twice!

I suppose I just thought that being to access all nodes that have an edge that leads to a specific UID would be a route search function.

Imagine if for every such node I pointlessly added a string predicate (called billuidstring for example) with the edge UID as a string. I would be able to access that node by doing an eq() func for that UID (as a string)

What is a UID edge other than just another triple? Anyway your filter option will work fine, I just wanted to explain my thinking

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.