I have a modified LAMP stack using Neo4j. I am trying to return the node type from a query in my middleware to my client-side Javascript. I am using the Graphaware PHP library to handle authentication between my middleware (PHP) and my database.
I just need to know if there is something in the Cypher query language that has the node type in it: a function I can put in the RETURN clause, or something returned in the return object. For example, if I had the following query:
MATCH (a:Person)--(b:Institution)
WHERE a.name CONTAINS "David"
RETURN a,b;
is there anything I can add to the Cypher query so I can differentiate the node types "a" and "b" after they have returned to my Middleware? Right now I can't tell them apart without making a gross massive switch statement in my middleware that has attributes specific to each node type to tell them apart, or to write separate queries for every possible node to return (which does not scale well in my project).
The Neo4j Workbench seems to have this functionality since it automatically makes nodes of the same type the same color. But by looking at the JSON objects returned from queries in the workbench it does not seem to be obvious how the workbench is differentiating the different node types.
There is no need to return extra information as the Node object returned to the GraphAware php client contains the node labels.
You can just filter the Result's record to add the additional info to the response you pass to your frontend.
Example in PHP :
$result = $this->client->run('MATCH (n:Person)-[:WORKS_AT]->(c) WHERE n.name = {name} RETURN n, c', ['name' => 'Pol']);
$frontendResult = [];
foreach ($result->records() as $record) {
$frontendResult[] = array_map(function(Node $value) {
return [
'properties' => $value->values(),
'type' => $value->labels()[0]
];
}, $record->values());
}
print_r(json_encode($frontendResult, JSON_PRETTY_PRINT));
Which would produce the following json :
[
[
{
"properties": {
"name": "Pol"
},
"type": "Person"
},
{
"properties": {
"name": "Acme"
},
"type": "Company"
}
]
]
The labels()[0]
is assuming you expect only one label on your nodes, which of course is not always true in standard neo4j applications.