3

Currently have a graph schema defined with a single node type and a single relationship type. My goal is to pass a particular subgraph to a graph visualization utility for display, so I need to get all nodes and all relationships between the nodes. More particularly, the subgraph is the subgraph beginning at node n and extending max_steps edges from n.

So far, I have something along the lines of:

class Position
  include Neo4j::ActiveNode

  has_many :out, :next, relationship_class: :To

  def get_next_subgraph( max_steps = 1 )
    nodes = []
    edges = []

    data = self.as(:n)
           .next(:m, :r, rel_length: max_steps )
           .pluck(:m, :r)

    data.each do |n, r|
      nodes.append n
      edges.append r[0]
    end

    return {
        nodes: nodes,
        edges: edges
    }

  end  
end

which gets used in something like the following manner:

pos = Position.find('24325')
sg = pos.get_next_subgraph()

sg[:edges].each do |edge|
  edge.from_node.uuid
end

The issue I'm encountering is that because from_node and to_node are lazy loaded, neo4j.rb performs an additional query each time I try to access the uuid of a particular relationship, which is needed to identify the node at each end of the edge. So, what I need is to either have the from_node and to_node mapped to the corresponding nodes (which are returned with the query already) or another way to retrieve the from_node/to_node uuids.

I have a few other ideas about how to do this, but I've spent enough time on something that seems to be a common enough pattern that I feel like I must be missing something.

So, in short, what's the correct/idiomatic way to use neo4j.rb to retrieve a subgraph in a single query?

(For context, this will eventually be sent back in response to a GraphQL query.)

bstovall
  • 101
  • 1
  • 8
  • Does this other answer help? It sounds similar. https://stackoverflow.com/questions/16101789/extract-subgraph-in-neo4j?rq=1 (I've never seriously used neo4j; I'm not suggesting they're the same question) – Jared Beck Feb 24 '20 at 04:36
  • @JaredBeck - No, because my question has to do specifically with the neo4j.rb Ruby Gem, which is basically an ORM for Neo4j. I know how to accomplish what I need directly in Neo4j or using the low-level API provided by neo4j.rb, but not the high-level API. Like I mentioned, I can accomplish this using other techniques, but I'd much prefer to figure out how to query and retrieve paths using the high-level API if there's an appropriate way to do it. – bstovall Feb 25 '20 at 04:46

0 Answers0