How could I batch check whether nodes exist?

How could I batch check whether nodes exist?

Checking the existence of a node is actual not a easy as one would expect.

For example on a empty dataset running query:

query {
  nodes(func: uid(0x2a, 0x2b)) {
    uid
  }
}

You will get:

{
  "data": {
    "nodes": [
      { "uid": "0x2a" },
      { "uid": "0x2b" }
    ]
  }
}

Which you wpuld think means there predicates for these nodes, but there are not. In this sense nodes ALWAYS exist.

So the better way to do it, would be to check for a predicate which you know should exist. But this may not always be accurate either because DQL is loosely typed and even the dgraph.type could be missing.

But that is really the only way to sort of do this…

query {
  nodes(func: uid(0x2a, 0x2b)) @filter(has(dgraph.type)) {
    uid
  }
}

But know that if you have a node without a type it will not be returned.

2 Likes