Dgraph if else function?

Does dgraph have a if-else function?

If author is same as original author, return false for knows flag.

How to achieve this requirement?

case when?
foreach?
while?

No, dgraph doesn’t have a direct if-else. But there is a different approach to achieve what you want. You would want to read about Query Variables, Value Variables and probably Math on value variables. If you could ask a more specific question then I’d be able to help you write the query. In your question, I am not sure about what is “original author”, is it a known string? What is “knows flag”?

@ahsan
the relationship like this:
image

I need to calculate:

Given a Message, retrieve the (1-hop) Comments that reply to it.
In addition, return a boolean flag knows indicating 
if the author of the reply knows the author of the original message. 
**If author is same as original author, return false for knows flag.**

What should I do to get a knows flag?

@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
              }
            ]
          }
        ]
      }
    ]
  }

@ahsan
wonderful!thank you!

1 Like