cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher Query String Problem

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!

4 REPLIES 4

Your initial value of path inside reduce() is '1', a string, but your expression is attempting to concatenate that with integers provided by index.Index. Have you tried wrapping that part in toString() to ensure you're concatenating two strings?

Thanks for the suggestion Terry.

I changed my query to:
"""MATCH p = (this)<-[:CHILD_OF *0..]-(c:Part) with *, relationships(p) as i
RETURN REDUCE (path = '1', index IN i | path + ',' + toString(index.Index)) AS path, c.name as name
ORDER BY path"""

I'm still getting the same complaint in the playground:
"message": "String("1") (of class org.neo4j.values.storable.StringWrappingStringValue)",

Is it strange that the same query will run fine in neo4j and not graphql playground?

I think the problem might be your GraphQL schema indicates that querying tree will return a list of Part objects ([Part]) -

but your cypher is returning a simple string.

You could try changing your schema to expect a string in return:

tree(root: String): String

or adjust your return statement to return data that matches the Part schema:

RETURN { name: REDUCE (path = '1',...) }

My neo4j query correctly returns Tree fields so I tried:
type Query {
tree(root: String):[Tree]
@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"""
)
}

I'm still getting the error
"message": "String("1") (of class org.neo4j.values.storable.StringWrappingStringValue)"

so what exactly is "class org.neo4j.values.storable.StringWrappingStringValue" ?

tree(root: String): String didn't work.