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.)