Query for nodes that do not have predicate with connection to specific user node

  • I have a node (type “Post”).
  • That node is connected to many images.
  • Each image can have comments.
  • Each comment can be connected to one user.

I need to query for all images. Each image returned should not have a comment that is from the user with uid 0x1.

Additionally, I am trying to achieve the results without using vars?

Here is the query that I have come up with so far.

Query
{
    query(func: uid("0x5")) {
        uid
        IsOwnedBy {
          uid
          username
        }
        hasImages @filter(has(hasComments)) {
          uid
          filename
          hasComments {
            uid
            text
            commentBy @filter(not uid("0x1")) {
              uid
              username
            }
            dgraph.type
          }
          created
          dgraph.type
        }
        created
        dgraph.type
    }
}

If I add not to @filter(not has(hasComments)), then it shows me all the images that does not have any comments. But I want images with or without comments, just not images that have comments from user 0x1.

If I have not on the commentBy @filter(not uid("0x1")) { filter, then I do not see the commentBy edge, but I still see the images that have comments from that user.

ImagesHaveComments

Dgraph metadata

v23.1.0

The only way I see to do this without using variables is to filter the results client side.

That sound like a perfect case for vars. To capture some information or UIDs and use it elsewhere in the query.

I am starting to agree. I have tried many permutations and even had ChatGPT suggest some options and while some have come close, nothing has been as effective as using variables. Not a big deal. I was just wondering if it was possible.

Why? What reasoning do you have for not using var blocks? Using var blocks will absolutely increase performance of your queries.

No good reason. Just learning still and seeing what is possible. That is good to know that using vars will help with performance.