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.

Help Needed to Define Schema

I have a simple tree structure consisting of Nodes and "CHILD_OF_NODE" relationships
(Node)<-[CHILD_OF_NODE]-(Node)

My Server Schema:

type Node {
	ID: ID!
	name: String
}

type branch @relation(name: "CHILD_OF_NODE"){
	from:[Node]
	to:[Node]
	ID:String
	index: Int
}

type Query {
	tree(root: String):[branch]
		@cypher(statement:"""
			MATCH path = (:Node{name:$root})<-[branch:CHILD_OF_NODE*1..]-(to:Node)
				with to,nodes(path)[-2] as from, last(branch) as branch
				RETURN branch{.ID, .index, to, from}
		""")

I can run the cypher sucessfully in the Neo4j browser, but cannot query with Graphql Playground.

{tree(root: "Root"){
  from{ID name}
  to{ID name}
	ID
  index
}}

The following is returned.

"stacktrace": [
            "TypeError: Cannot read property 'value' of undefined",
            "    at getRelationTypeDirective (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/utils.js:775:7)",
            "    at buildCypherSelection (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/selections.js:311:68)",
            "    at customQuery (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/translate.js:825:68)",
            "    at translateQuery (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/translate.js:671:12)",
            "    at cypherQuery (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/index.js:229:40)",
            "    at _callee$ (/home/rchevalier/dev/grand-stack/api/node_modules/neo4j-graphql-js/dist/index.js:103:31)",
            "    at tryCatch (/home/rchevalier/dev/grand-stack/api/node_modules/@babel/runtime-corejs2/node_modules/regenerator-runtime/runtime.js:63:40)",
            "    at Generator.invoke [as _invoke] (/home/rchevalier/dev/grand-stack/api/node_modules/@babel/runtime-corejs2/node_modules/regenerator-runtime/runtime.js:293:22)",
            "    at Generator.next (/home/rchevalier/dev/grand-stack/api/node_modules/@babel/runtime-corejs2/node_modules/regenerator-runtime/runtime.js:118:21)",
            "    at asyncGeneratorStep (/home/rchevalier/dev/grand-stack/api/node_modules/@babel/runtime-corejs2/helpers/asyncToGenerator.js:5:24)"
          ]

There is certainly something wrong with my schema. I've tried dozens of combinations to no avail. Any help is appreciated.

5 REPLIES 5

I opened the file mentioned in the first line of the stack trace. It seems I'm getting an error here:

var getRelationTypeDirective = function getRelationTypeDirective(relationshipType) {
  var directive = relationshipType && relationshipType.directives ? relationshipType.directives.find(function (e) {
    return e.name.value === 'relation';
  }) : undefined;
  return directive ? {
    name: directive.arguments.find(function (e) {
      return e.name.value === 'name';
    }).value.value,
    from: directive.arguments.find(function (e) {
      return e.name.value === 'from';
    }).value.value,
    to: directive.arguments.find(function (e) {
      return e.name.value === 'to';
    }).value.value
  } : undefined;
};

This makes me think that my cypher query is not returning the correct shape. This is what my cyper returns in the neo4j browser. Does this look right?

{"index":"1","from":{"name":"Root","ID":"8e21d55c-a9f1-42d6-86be-395956b4a21b"},"ID":"15dd5565-b97f-4fd1-b6ae-498244e8b190","to":{"name":"Grapplegrommet Assembly","ID":"dad4996│
│0-0ec7-40ca-8c26-a754bdc04484"}}

MuddyBootsCode
Graph Steward

So this is directly from the docs:

type User {
    userId: ID!
    name: String
    rated: [Rated]
}

type Rated @relation(name: "RATED") {
    from: User
    to: Movie
    rating: Float
    review: String
}

In your provided example:

type Node {
	ID: ID!
	name: String
}

type branch @relation(name: "CHILD_OF_NODE"){
	from:[Node]
	to:[Node]
	ID:String
	index: Int
}

On your Node type you're not specifying the relation anywhere. I think it would need to be something like:

type Node {
   ID: ID!
   name: String
   childOf: CHILD_OF_NODE
}

Would work I think.

Thanks for the response Michael.

Sadly the server crashes with Error: Unknown type "CHILD_OF_NODE"

This will yield a nice listing of the parents and children of each node:

type Node {
	ID: ID!
	name: String
	parent:[Node] @relation(name: "CHILD_OF_NODE", direction: IN) 
	child:[Node] @relation(name: "CHILD_OF_NODE", direction: OUT) 
}

Like so:

{Node{
  name
	parent{name}
  child{name}
}}


{
        "name": "Assy 2",
        "parent": [
          {
            "name": "Part 2"
          },
          {
            "name": "Part 1"
          }
        ],
        "child": [
          {
            "name": "Assy 5"
          },
          {
            "name": "Assy 1"
          }
        ]
      },

But I'm not seeing how that helps me to return a branch, which is:

Parent[Node],index,ID,Child[Node]

The tree query returns the tree nicely in Neo4j but not in the the playground.

MATCH path = (:Node{name:"Assy 1"})<-[branch:CHILD_OF_NODE*1..]-(to:Node)
				with to,nodes(path)[-2] as from, last(branch) as branch
				RETURN branch{.ID, .index, to, from}


One row of Neo4j browser results:
{
"index":"1",
"from":{"name":"Assy 1","ID":"79a862cf-91b1-45e8-8681-205f6488a631"},
"ID":"ccc02669-9df9-4564-b787-dac0ab6e1802",
"to":{"name":"Assy 2","ID":"e8dbbb3c-d8c7-4738-a9a7-f09ea27450e2"}
}

MuddyBootsCode
Graph Steward

You're more than likely going to have to write it using the @cypher directive and do some custom logic.