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.

Nested Query is Undefined

I'm trying to return the Node object in my tree query.

type Node {
	ID: ID!
  name: String
  child: [Child]
}

type Child @relation(name: "CHILD_OF") {
	from: Node  #child 	
	to: Node    #parent
	index: String
}

type Tree {
	path: String
	name: String
	indent: Int
	childCount: Int
	node:[Node]  // return the Node object 
}

type Query {
	tree(root: String):[Tree]
		@cypher(statement:
			"""MATCH p = (:Node { name: $root })<-[:CHILD_OF *0..]-(c:Node) 
			WITH c, apoc.text.join('1' + [rel in relationships(p) | rel.index], '.') as path, size((c)<-[:CHILD_OF]-()) as childCount, c as Node
			ORDER BY path
			RETURN c { .name, path, childCount, indent: size(split(path,'.'))-1, Node}"""
		)
}

Running the following in the playground:

query
	{tree(root: "Root"){
    path
    indent
    childCount
    node{
      name
    }
	}
}

Returns the following:

{
  "errors": [
    {
      "message": "Cannot read property '0' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "tree"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property '0' of undefined",
            "    at buildCypherSelection (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:315:20)",
            "    at recurse (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:87:33)",
            "    at buildCypherSelection (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:173:12)",
            "    at recurse (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:87:33)",
            "    at buildCypherSelection (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:173:12)",
            "    at recurse (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:87:33)",
            "    at buildCypherSelection (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:173:12)",
            "    at customQuery (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/translate.js:558:68)",
            "    at translateQuery (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/translate.js:501:12)",
            "    at cypherQuery (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/index.js:141:40)"
          ]
        }
      }
    }
  ],
  "data": {
    "tree": null
  }
}

Any ideas what I'm doing wrong?

6 REPLIES 6

MuddyBootsCode
Graph Steward

You need to specify the direction of the relationship in your schema in both type definitions.
So like:


type Tree {
	path: String
	name: String
	indent: Int
	childCount: Int
	node:[Node] @relation (name: "relationshipname", direction: "<out or in>") // return the
}

And do the same on your node.

No joy I'm afraid Michael.

type Child @relation(name: "CHILD_OF") {
	from: Node  #child
	to: Node    #parent
	index: String
}

type Node{
	ID: ID!
  name: String
  child: [Child]
  trees: [Tree] @relation(name: "IN_BRANCH", direction: "IN")
}

type Tree {
	path: String
	name: String
	indent: Int
	childCount: Int
	node: [Node] @relation(name: "IN_BRANCH", direction: "OUT")

}

type Query {
	tree(root: String):[Tree]
		@cypher(statement:
			"""MATCH p = (:Node { name: $root })<-[:CHILD_OF *0..]-(c:Node) 
			WITH c, apoc.text.join('1' + [rel in relationships(p) | rel.index], '.') as path, size((c)<-[:CHILD_OF]-()) as childCount, c as node
			ORDER BY path
			RETURN c { .name, path, childCount, indent: size(split(path,'.'))-1, node}"""
		)
}

Returns:

{
  "errors": [
    {
      "message": "Expected to find a node at '  tree@388' but found Map{name -> String(\"Root\"), path -> String(\"1\"), childCount -> Long(3), nodes -> (109169), indent -> Long(0)} instead",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "tree"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": "Neo.DatabaseError.Statement.ExecutionFailed",
          "name": "Neo4jError",
          "stacktrace": [
            "Neo4jError: Expected to find a node at '  tree@388' but found Map{name -> String(\"Root\"), path -> String(\"1\"), childCount -> Long(3), nodes -> (109169), indent -> Long(0)} instead",
            "",
            "    at captureStacktrace (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/result.js:199:15)",
            "    at new Result (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/result.js:65:19)",
            "    at _newRunResult (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/transaction.js:354:10)",
            "    at Object.run (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/transaction.js:238:14)",
            "    at Transaction.run (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/transaction.js:104:26)",
            "    at /home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/index.js:102:25",
            "    at TransactionExecutor._safeExecuteTransactionWork (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:134:22)",
            "    at TransactionExecutor._executeTransactionInsidePromise (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:122:32)",
            "    at /home/rchevalier/dev/grand-stack/api/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:61:15",
            "    at new Promise (<anonymous>)"
          ]
        }
      }
    }
  ],
  "data": {
    "tree": null
  }

Still beatin' my head against the wall with this one.

I'm beginning to wonder if the cypher query is returning the correct form.

type Child @relation(name: "CHILD_OF") {
	from: Node  #child
	to: Node    #parent
	index: String
}

type Node{
	ID: ID!
  name: String
  child: [Child]
  branch: [Tree] @relation (name:"BRANCH_OF_NODE", direction: "IN")
}

type Tree {
	path: String
	indent: Int
	childCount: Int
	Node:[Node] @relation (name: "BRANCH_OF_NODE", direction: "OUT")
}

type Query {
	tree(root: String):[Tree]
		@cypher(statement:
			"""MATCH p = (:Node { name: $root })<-[:CHILD_OF *0..]-(c:Node) 
			WITH c, apoc.text.join('1' + [rel in relationships(p) | rel.index], '.') as path, size((c)<-[:CHILD_OF]-()) as childCount, c as Node
			ORDER BY path
			RETURN  c {path, indent: size(split(path,'.'))-1, childCount, Node}"""
		)
}

If I run the query in the neo4j browser it is returned in this form:

{
  "path": "1",
  "childCount": 2,
  "indent": 0,
  "Node": {
    "name": "Root",
    "ID": "7e460fd4-92aa-41d5-8b1c-a5501b48dcdd"
  }
}

Is it correct to expect the same result when running the following in the playground?

query {tree(root: "Root"){
	path
	indent
	childCount
  	Node{
    		ID
		name
  	}
	}
}

Instead, this mess is returned:

{
  "errors": [
    {
      "message": "Expected to find a node at '  tree@379' but found Map{path -> String(\"1\"), childCount -> Long(2), indent -> Long(0), Node -> (109169)} instead",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "tree"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": "Neo.DatabaseError.Statement.ExecutionFailed",
          "name": "Neo4jError",
          "stacktrace": [
            "Neo4jError: Expected to find a node at '  tree@379' but found Map{path -> String(\"1\"), childCount -> Long(2), indent -> Long(0), Node -

Did you find a solution to this issue?

If yes, can you share it, because I may be dealing with a similar problem here.

If not, something that can help, but not solve your issue, is to start the server with that:

DEBUG=neo4j-graphql-js npm "whatever you use to start your server"

You will be able to see at node's console the exact cypher code sent to the neo4j server, what may bring some light to the issue.

No, never did find a solution. I gave up on it.

Thanks for your suggestion. I tried it, but the error dump does not appear to have changed.

I beg your pardon Alex, I misunderstood your post. I see now that the terminal window where I am running the server shows the detailed output.

In the case of my query, it appears that neo4j-graphql-js doesn't send anything to the neo4j server so there is no output to read.

I'm sure being able to see the cypher code will be helpful in the future.