It looks like a primary use case for me when querying is going to involve
a) obtain a graph projection containing the information the user wants to see
b) format this into a custom JSON format so that the client UI can visualize this directly.
The dataviz format the client UI uses is a collection of nodes, each with id and data attributes, and a collection of edges with from and to attributes referring to the id values in the nodes
{
nodes: [
{id: id1,
data:{...}
},
{id: id2,
data:{...}
},
...
],
edges:[
{from:id1, to:id2},
...
]
}
I've got some way towards achieving this with a Cypher query (see example below), but the node format isn't right. I require, for example
{id: EDI,
data: {
name:'Edinburgh'
}
}
My questions are:
i) can this be done?
ii) should I be attempting to do this in the Cypher query, or am I better leaving this as post-processing in the software that makes the call to Neo4J? I'm thinking things like Neo4J excels at graph operations and adding result formatting is piling extra work onto the database engine that can be done just as easily by, say, a Java program.
Example:
CREATE (s1:Station {stationCode:'EDI', name:'Edinburgh'})-[ts1:TRAIN_SERVICE]->(s2:Station {stationCode:'NCL', name:'Newcastle'})-[ts2:TRAIN_SERVICE]->(s3:Station {stationCode:'YRK', name:'York'})
CREATE (s1:Station {stationCode:'GLW', name:'Glasgow'})-[ts1:TRAIN_SERVICE]->(s2:Station {stationCode:'CAR', name:'Carlisle'})-[ts2:TRAIN_SERVICE]->(s3:Station {stationCode:'PNR', name:'Penrith'})
MATCH (s1:Station)-[:TRAIN_SERVICE]->(s2:Station)-[:TRAIN_SERVICE]-(s3:Station)
CALL apoc.create.vRelationship(s1, 'ROUTE_EXISTS', {from:s1.stationCode, to:s3.stationCode}, s3) YIELD rel
WITH COLLECT(s1)+COLLECT(s3) AS stations, COLLECT(rel) AS existingRoutes
RETURN {nodes:stations, edges:existingRoutes}
This returns a collection of nodes and edges, as follows:
{
"nodes":[
{"name":"Glasgow","stationCode":"GLW"},
...
],
"edges":[
{"from":"GLW","to":"PNR"},
{"from":"EDI","to":"YRK"}
]
}