Conditional Query Statement

What I want to do

How to write a conditional query statement similar to the conditional upsert statement ?
I have two query statements, if the result set of the first query is null then the second query should be executed else I use the former.

What I did

I tried using the results from the previous query statement and apply filter to it and decide whether to execute the later query. But this doesn’t work.

1 Like

I am not sure your programming language of choice or requirements, but if your requirements are you must have two queries made at the same commit level (read timestamp) then you could make a read-only transaction and make both queries from that (the second being conditional of the first).

If you have a requirement of exactly one network request for some reason, you might be able to try

{
  q1(func: has(namex),first:1){x1 as uid}
  q2(func: has(name),first:1) @filter(eq(len(x1),0)) {uid}
}

where you will get q1 only, unless its empty, then you will get q2 only.

3 Likes

Thanks for the suggestion. That really helped! @iluminaeN