Returning Result from Custom Procedure

I'm trying to learn to write my own procedures and I'm stuck on what to return and how to stream it.

For example:

@Procedure(name = "myProcedure.loadData", mode = Mode.WRITE)
@Description("Call myProcedure.loadData(theData, theParam)")
public Stream<[Whatever]> makeDataGraphs(
     @Name("theData")  String theData,
     @Name("theParam") String param
     ) throws InterruptedException {
     
     List<Node> childNodes = parseChildNodes(theData);
     Node parentNode = parseParentNode(theData);
          
     for (Node c : childNodes) {
          Relationship r = parentNode.createRelationshipTo(c, HAS_CHILD);
 
          [ Should I add this relationship to a list or something? ]

          }

     private Node parseParentNode(Object data) {
          // do things
          Node parent = db.createNode(Parent);
          return parent;
     }

     private List<Node> parseChildNodes(Object data) {
          // do things
          List<Node> children = new ArrayList();
          for (String s : someList) {
               Node c = db.createNode(Child);
               c.setProperty("name", s);
               children.add(c);
          }
          return children;
     }

     someIterable [ How do I collect up the parent and children? ]

     return someIterable.stream();

}

What result class should I use return [Whatever] ?
Should I be collecting Maps of {"parent": parent, "has": r, "child": child} "
Do I need to return a stream or could I just return boolean upon success?

You can easily return a primitive, but you have to define a result class, that also identifies the name you should YIELD in your query, e.g.

public class LongResult {
    public Long out;

    public LongResult(Long value) {
        out = value;
    }
}

And your function would return Stream.of(new LongResult(nodeCount)); This presumes you're tracking nodeCount.

You can also easily return nodes or rels that way. If you want to return full paths, to be honest I'm not quite sure how to do this. What I've done is create a separate function that relies on a TraversalDescription to return a Stream<PathResult>:

public class PathResult {
    public Path path;

    public PathResult(Path path) {
        this.path = path;
    }
}
1 Like