Neo4j-graphql-js - creating relation between same label doesn't generate mutation

relationship

(Shreyansh Zazz) #1

You can find the schema at https://github.com/thepiperpied/palm-tree/blob/master/graphql-schema.js
You can find the main index.js at https://github.com/thepiperpied/palm-tree/blob/master/app.js

Here's what I want to do:

  • Create a relationship between to Departments

  • In cipher it's like : MATCH (a:Department{id: id}, b:Department{id: id}) CREATE a-[:CHILD]->b RETURN a, b

  • The auto generated mutation through /* augmentSchema will add autogenerated mutations based on types in schema */ const augmentedSchema = augmentSchema(schema); doesn't create what I need.

  • It's only providing me with a mutation i.e. AddDepartmentChildren( departmentid: ID! ): Department


(William Lyon) #2

Hi Shrey -

This seems to be a bug in neo4j-graphql-js when generating mutations for relationships connecting nodes with the same label. I've created an issue to track it here: https://github.com/neo4j-graphql/neo4j-graphql-js/issues/102

In the mean time, you can add the mutation to accomplish creating the relationship by adding this to your GraphQL schema:

type Mutation {
  AddDepartmentChildren(fromDepartmentID: ID!, toDepartmentID: ID!): Department @cypher(
statement:"""
  MATCH (from:Department {id: $fromDepartmentID})
  MATCH (to:Department {id: $toDepartmentID})
  CREATE (from)-[:CHILD_DEPT]->(to)
""")

}

Also, you have an error in your schema for the children field on the Department type, it should be:

  children: [Department] @relation(name: "CHILD_DEPT", direction: "OUT")

instead of using the @cypher directive there, use @relation to express the relationship


(Shreyansh Zazz) #3

Got it, Thanks man.

My love for Neo4J is increasing day by day.


(Shreyansh Zazz) #4

Where should I put the parameters that I want in return if the relationship is successfully created and where should I define the properties for the relation CHILD_DEPT?


(William Lyon) #5

Oh right, that mutation should return a Department node and I forgot the return clause. Our convention for the generated mutations has been to return the from node. So the query should be

MATCH (from:Department {id: $fromDepartmentID})
MATCH (to:Department {id: $toDepartmentID})
CREATE (from)-[:CHILD_DEPT]->(to)
RETURN from

To add properties to the relationship just include them as parameters of the mutation and update the @cypher query accordingly. Here's an example:

type Mutation {
  AddDepartmentChildren(fromDepartmentID: ID!, toDepartmentID: ID!, count: Int): Department @cypher(
  statement:"""
     MATCH (from:Department {id: $fromDepartmentID})
     MATCH (to:Department {id: $toDepartmentID})
     CREATE (from)-[r:CHILD_DEPT]->(to)
     SET r.count = $count
     RETURN from
    """)

Note that the neo4j-graphql-js library (and the neo4j-graphql database plugin) do not currently support an automatic way of working with relationship properties so you'll have to define @cypher directives on fields to work with relationship properties.


(Shreyansh Zazz) #6

Okay seems good to me. Thank you so much.