Conditional Query to Get full graph


(Avinash Jain) #1

I have a graph database like below
Country -> hasRegion -> Region -> hasCity -> City

Conditional query :
India -> hasRegion
For each Region we got(Region1,Region2)
Check if Region1 > hasOtherName > ?
Found
use node we got from hasOtherName for hasCity call
Not Found
use Region node for hasCity call.

How to write this query, Please help me with syntax.


(Shanea) #2

I have some clarifications to ensure I understand what you are asking.

Is this the set up you have?

If it is correct that you are set up this way, do you want to check if region has no Jharkhand then use West to find city?


(Avijain7143) #3

Yes, that’s correct.


(Shanea) #4

So I don’t think you can solve your problem as you have it with the functions that are available. I think a better way to structure would be like this so no matter which name of the region you can get to hasCity. Does this make sense?


(Avinash Jain) #5

I have re format the case for better understanding :

GRAPH :

		India  > hasRegion > East > hasCity > Kolkatta
		India  > hasRegion > West > hasCity > Pune
		India  > hasRegion > West > hasCity > Mumbai 
		India  > hasRegion > West > hasCity > Nagpur
		India  > hasRegion > RegionWest > hasCity > Nagpur

		RegionWest > hasPivotNode > West

Cases :

		Case1 : Cities of region RegionWest : 
		   RegionWest > hasPivotNode > (Found) West > hasCity ?
		   Expected Result :- Pune, Mumbai, Nagpur
						   
		Case2 : Cities of region East : 				   
		   East > hasPivotNode > (Not Found) East > hasCity ?
		   Expected Result :- Kolkatta

I need single conditional query for both of the above cases.

Note : I don’t have any scope to re model the graph.


(Avinash Jain) #6

@shanea please see if you can help me anyway.


(Michel Conrado (Support Engineer)) #7

For your case I believe that Recurse Query is ideal. We have nothing to do logical (conditional in this case) verification in the query. Unless you make a var block scheme (“waterfall” blocks). Or maybe use the logic found in math operations some how.

The most recommended would be you to expand your structure using @Recurse, hence in your application you control the logic of conditions.

https://docs.dgraph.io/query-language#recurse-query


(Michel Conrado (Support Engineer)) #8

Hey, check out this query and see if it works as intended.

{
  var(fun: has(hasRegion) @filter(eq(country, "India"))){
  name
  Region @filter( has(hasOtherName)) {
    R1 as uid
    name
    hasOtherName { R2 as uid }
  }
   RegionB2 : Region @filter(Not has(hasOtherName)) {
    R1a as uid
    name

  }
    Case1(fun: uid(R1, R2)){
      name
    }
    Case2(fun: uid(R1a)){
      name
    }
}