cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

APOC subgraphAll return JSON with a list of the direct relations and their nodes

Good morning,
I am having some trouble with a cypher query that uses the APOC subgraphAll procedure.
I want to return a simple JSON list with the relations that are present in my subgraph and the nodes they relate, how can I obtain this?
I am trying to return a list before converting it to JSON (to verify that the returned data is what I actually want) using the UNWIND function but at the moment it is relating every node with every relationship. This is my cypher query so far:

// Extract a subgraph List 
MATCH (w:Column{name:'MYCOLUMN',env:'MYENV'})
CALL apoc.path.subgraphAll(w,{maxLevel:5,relationshipFilter:'LABEL1|LABEL2|LABEL3|LABEL4',labelFilter:'+Identifier|+Column',bfs:true}) YIELD nodes, relationships
UNWIND relationships AS r
UNWIND nodes AS n
RETURN w.name AS Name, n.name AS Name2, type(r) AS RelationType,  labels(n) AS NodeLabel

Thank you very much for the help.

Regards,
Gustavo.

4 REPLIES 4

You may want to just work with the relationships themselves, and use startNode(r) and endNode(r) to get the start and end nodes for each relationship.

Thank you for the response Andrew, do you have any suggestion on how to do this?
I originally chose to use the subgraph APOC because I am actually getting multiple nodes and relations from the start node to the fifth hop. My objective is simply to list the node-relation-node in a list to then convert it to a JSON list and return it to my program.
(in the example I am limiting the level to 5 for testing purposes only, ultimately it will go all the way to the edge of my graph)

Can you be more specific about what's tripping you up?

You have the relationships list, you can use startNode(r) and endNode(r) to get references to the start and end nodes of each relationship. You can either UNWIND the relationships list and work with it that way, or use list comprehension (or extract()) to transform your list.

Thank you Andrew, I was missing the startNode(r) and endNode(r) functions.
Thank you for pointing them out!