cancel
Showing results for 
Search instead for 
Did you mean: 

display the neo4j data in react js application

Shweta19
Node

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 ?

 

 

 

 

 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

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

View solution in original post

8 REPLIES 8

glilienfield
Ninja
Ninja

Can you share you query and driver code. 

import { Body, Controller, Get, Post } from "@nestjs/common";
import { EmployeeService } from "./employee.service";
import { Neo4jService } from 'nest-neo4j'


@Controller('employee')
export class EmployeeController {
constructor(private readonly employeeService: EmployeeService , private readonly neo4jService: Neo4jService) {

}

@Post()
addEmployee(@Body('FirstName') fname : String, @Body('LastName')lname : String😞 any {
const data = this.employeeService.addEmployee(fname , lname);
return {newEmployee : data}
}

@Get()
getAllNodes() {
const res = this.neo4jService.read(`MATCH (n) RETURN n`)
return res;

}



}

You getAllNodes query is only returning the nodes. This will not include relationships. What is it you want to return?

I want to return all the nodes along with relationships.

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. 

glilienfield
Ninja
Ninja

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

 

 

 

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

glilienfield
Ninja
Ninja

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