I have a simple Bill of Materials DB
CREATE (p1:Part{name:"Root"})
CREATE (p2:Part{name:"Sub Assy 1"})
CREATE (p3:Part{name:"Sub Assy 2"})
CREATE (p4:Part{name:"Sub Assy 3"})
CREATE (p5:Part{name:"Part 1"})
CREATE (p6:Part{name:"Part 2"})
CREATE (p1)<-[:CHILD_OF{Index:1}]-(p2)
CREATE (p1)<-[:CHILD_OF{Index:2}]-(p3)
CREATE (p1)<-[:CHILD_OF{Index:3}]-(p4)
CREATE (p2)<-[:CHILD_OF{Index:1}]-(p5)
CREATE (p2)<-[:CHILD_OF{Index:2}]-(p6)
CREATE (p3)<-[:CHILD_OF{Index:1}]-(p2)
This query gives the expected output in the neo4j browser
MATCH p = (root:Part { name:"Root" })<-[:CHILD_OF *0..]-(c:Part) with *, relationships(p) as i
RETURN REDUCE (path = '1', index IN i | path + '.' + index.Index) AS path, c.name as name
ORDER BY path
Result:
╒═════════╤════════════╕
│"path" │"name" │
╞═════════╪════════════╡
│"1" │"Root" │
├─────────┼────────────┤
│"1.1" │"Sub Assy 1" │
├─────────┼────────────┤
│"1.1.1" │"Part 1" │
├─────────┼────────────┤
│"1.1.2" │"Part 2" │
├─────────┼────────────┤
│"1.2" │"Sub Assy 2" │
├─────────┼────────────┤
│"1.2.1" │"Sub Assy 1" │
├─────────┼────────────┤
│"1.2.1.1" │"Part 1" │
├─────────┼────────────┤
│"1.2.1.2" │"Part 2" │
├─────────┼────────────┤
│"1.3" │"Sub Assy 3" │
└─────────┴────────────┘
Using the grand stack api:
I defined the following types and query in my schema
type Part {
name: String
}
type Tree {
path: String
name: String
}
type Query {
tree(root: String):[Part]
@cypher(statement:
"""MATCH p = (this)<-[:CHILD_OF *0..]-(c:Part) with *, relationships(p) as i
RETURN REDUCE (path = '1', index IN i | path + ',' + index.Index) AS path, c.name as name
ORDER BY path"""
)
}
GraphQL Playground:
query{tree(root: "Root"){name}}
Returns:
Errors": [
{
"message": "String("1") (of class org.neo4j.values.storable.StringWrappingStringValue)",
so it doesn't seem to like the '1' string supplied for path = '1'
Desperately trying to get something to work I try:
type Query {
tree(root: String):[Part]
@cypher(statement:
"""MATCH p = (this)<-[:CHILD_OF *0..]-(c:Part) with *, relationships(p) as i
RETURN c"""
)
}
Which at least returns something:
{
"data": {
"tree": [
{
"name": "Root"
},
{
"name": "Sub Assy 1"
},
{
"name": "Sub Assy 1"
},
{
"name": "Sub Assy 1"
},
{
"name": "Sub Assy 1"
},
.....
So I can only conclude that there is something wrong with '1' in this line of my @cypher statement
RETURN REDUCE (path = '1', index IN i | path + ',' + index.Index) AS path, c.name as name
HELP PLEASE!