Dgraph if else function?

@youyin123,

I replicated your model by this mutation:

{
  set{
    _:p1 <name> "person1" .
    _:p1 <knows> _:p2 .
   
    _:p2 <name> "person2" .
    _:p2 <knows> _:p1 .
    
    _:c1 <text> "comment1" .
    _:c1 <hasCreator> _:p2 .
    
    _:m1 <text> "message1" .
    _:m1 <hasCreator> _:p1 .
    _:m1 <replyOf> _:c1 .
  }
}

and what you want can be achieved by this query:

{
  var(func: eq(text, "message1")){
    u as uid
  	hasCreator{n as name}
  	replyOf {
      text
      hasCreator {
        knows 
      }
    }
  }
  f(func: uid(u)){
    replyOf {
      hasCreator {
        c as count(knows) @filter(eq(name, val(n)))
        knows: math(c == 1)
      }
    }
  }
}

By “Given a message”, I assume that the text of the message is given. It can be the uid, or any other field as well. You can use whatever identifier you like.

The response is like this:

"data": {
    "f": [
      {
        "replyOf": [
          {
            "hasCreator": [
              {
                "count(knows)": 1,
                "knows": true
              }
            ]
          }
        ]
      }
    ]
  }