JSON Tree Representation of a DAG GSQL Result

The GSQL query below calculates the reachable DAG (Direct Acyclic Graph) for a person node, namely, all the persons that are reachable as direct or indirect friends of a given person, for any level of indirection (unlimited hops). What I need now is to present the result as a JSON hierarchy that shows the nested levels of indirect relationships in the graph, so that for every person in the result graph, his or her direct friends are shown as directly nested JSON structures. In Gremlin, that query can be written with an explicit tree accumulator: repeat(out(“friend”)).until(__.not(outE(“friend”))).emit().tree().
– How do we modify the GSQL query below to produce the JSON tree for the resulting DAG?
Thanks !!

CREATE QUERY reachableDAG(VERTEX p) FOR GRAPH Social {
SetAccum @@reachableEdges;
OrAccum @chosenEdge
Current= {p};

 WHILE (Current.size() > 0) DO
     Current = SELECT reachablePerson
                FROM Current:s -(Friend:e) -> reachablePerson
                WHERE e.@chosenEdge == false
                ACCUM e.@chosenEdge = true
                @@reachableEdges += e;
  END;
  PRINT @@reachableEdges;

}

I am not sure if this is a perfect answer, but its one suggestion

SetAccum < VERTEX > @directFriends;

WHILE (Current.size() > 0) DO
     Current = SELECT reachablePerson
                FROM Current:s -(Friend:e) -> reachablePerson
                WHERE e.@chosenEdge == false
                ACCUM e.@chosenEdge = true, s.@directFriends += reachablePerson
                @@reachableEdges += e;
        PRINT Current[Current.@directFriends];
  END;
  PRINT @@reachableEdges;

}

1 Like

Thanks for the reply. Yes that would give us an entire cross-sectional layer in the spanning tree result, which is helpful, but it does not give us the subsequent nesting inside each node in order to make a JSON hierarchy. This is something that is very easy to do with TinkerPop since they have path and tree “accumulators” tracking the traversal history or with Neo4J as they have path objects in the API that you can carry through your Java traversals, and it is a must if you want path or tree results. I think this comes from the notion in those graphs DB’s that a traversal is something that automatically gets recorded. I am not sure if we can do this with GSQL. Thanks !

1 Like