Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-05-2022 03:59 AM
Hi ,
I have created rest apis for CRUD operations in nodejs that interacts with the neo4j database. now i want to fetch this data and show the relations in react App. But the response im getting from the api shows no relation of the nodes. Can anyone help me out on this ?
Solved! Go to Solution.
06-05-2022 10:02 AM
sorry, last time. I forgot to include each relationship's type:
match(n)
optional match (n)-[o]->(out)
optional match (n)<-[i]-(in)
with n, count(out) as totalOutgoing, count(in) as totalIncoming,
collect({endNode: id(out), type: type(o), properties: properties(o)}) as outgoing,
collect({startNode: id(in), type: type(i), properties: properties(i)}) as incoming
return {id: id(n), labels: labels(n), properties: properties(n), relationships_out: outgoing, relationships_in: incoming, totalOutgoing: totalOutgoing, totalIncoming: totalIncoming} as node
06-05-2022 05:54 AM
Can you share you query and driver code.
06-05-2022 07:24 AM
06-05-2022 08:04 AM
You getAllNodes query is only returning the nodes. This will not include relationships. What is it you want to return?
06-05-2022 08:17 AM
I want to return all the nodes along with relationships.
06-05-2022 08:29 AM
Relationships are between two nodes. For each node, do you want a list of the relationship properties and the id of the other node? That would give you enough info to recreate the graph if needed, or show the use the pair of nodes, without send the nodes twice.
06-05-2022 09:46 AM - edited 06-05-2022 10:01 AM
There are lots of ways to package the data. I came up with this to give you an idea. I formatted the output as one single json object for each node. The object contains the node's id, is properties, a count of the number of incoming and outgoing relationships, and arrays containing the information for the incoming and outgoing relationships. Each relationships contains the id of the other node and its properties.
Alter it to meet your needs. Let me know what you need if it isn’t close.
As a note, the relationship data will be repeated twice in the response, once for each participating node. We can make this more efficient by having an array of the relationships (with their id, properties, and type) and then only including the relationship id's in the each node's array of relationships.
match(n)
optional match (n)-[o]->(out)
optional match (n)<-[i]-(in)
with n, count(out) as totalOutgoing, count(in) as totalIncoming,
collect({endNode: id(out), properties: properties(o)}) as outgoing,
collect({startNode: id(in), properties: properties(i)}) as incoming
return {id: id(n), properties: properties(n), relationships_out: outgoing, relationships_in: incoming, totalOutgoing: totalOutgoing, totalIncoming: totalIncoming} as node
06-05-2022 09:53 AM
I realized I did not include the labels of the node and I couldn't get editing to work, so created a new reply.
match(n)
optional match (n)-[o]->(out)
optional match (n)<-[i]-(in)
with n, count(out) as totalOutgoing, count(in) as totalIncoming,
collect({endNode: id(out), properties: properties(o)}) as outgoing,
collect({startNode: id(in), properties: properties(i)}) as incoming
return {id: id(n), labels: labels(n), properties: properties(n), relationships_out: outgoing, relationships_in: incoming, totalOutgoing: totalOutgoing, totalIncoming: totalIncoming} as node
06-05-2022 10:02 AM
sorry, last time. I forgot to include each relationship's type:
match(n)
optional match (n)-[o]->(out)
optional match (n)<-[i]-(in)
with n, count(out) as totalOutgoing, count(in) as totalIncoming,
collect({endNode: id(out), type: type(o), properties: properties(o)}) as outgoing,
collect({startNode: id(in), type: type(i), properties: properties(i)}) as incoming
return {id: id(n), labels: labels(n), properties: properties(n), relationships_out: outgoing, relationships_in: incoming, totalOutgoing: totalOutgoing, totalIncoming: totalIncoming} as node
All the sessions of the conference are now available online