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.

Using Merge Mutation

I want to use merge instead of create for making nodes. Since GraphQL does not make the merge statement for me, I am trying to make it myself.

This is what I am tying.

type Mutation {
  MergeLesson(id: ID!, name: String!, language: String!, dialect: String , description: String):Lesson!
    @cypher(
        statement: "MERGE(lesson:Lesson{id: $id, name: $name, language: $language, dialect: $dialect, description: $description})"
      )
}
mutation MergeLesson {
  MergeLesson (id: "1", name: "test", language: "English", 
    dialect: "USA", description: "Testing funtion"){
    name
    language
    dialect
    description
  }
}

and I Tryed

mutation MergeLesson($id: ID! $name: String!, $language: String!, $dialect: String!, $description: String!) {
  MergeLesson (id: $id, name: $name, language: $language, 
    dialect: $dialect, description: $description){
    name
    language
    dialect
    description
  }
}

I am having many problems and was wondering if there was a better way to do.

  1. I the ID is an issue. I have to put on in because it is required. However, I do not want to match on ID. I only want to match on name and language.
  2. I keep getting this error
 {
   "errors": [
    {
    "message": "Cannot return null for non-nullable field Mutation.MergeLesson.",

Should I keep trying to get it to work this way or try something else?

1 REPLY 1

William_Lyon
Graph Fellow

Hey @futuristnicole -

I think the issue here is that the Cypher statement in the @cypher schema directive needs to return the node. Also, it is usually a good idea to MERGE only on the properties that identify uniqueness (which in your case sounds like name and language?). So something like this:

type Mutation {
  MergeLesson(id: ID, name: String!, language: String!, dialect: String, description: String): Lesson! @cypher(statement:"""
   MERGE (lesson:Lesson {name: $name, language: $language})
   SET lesson.id = $id, lesson.dialect = $dialect, lesson.description = $description
   RETURN lesson
  """)
}

One issue here is you might get Cypher errors if dialect or description are not included as the cypher query expects those to be passed as parameters (but they won't be if they're not passed in arguments), so it might be better to use an input type for this:

input MergeLessonInput {
  id: ID, 
  name: String!, 
  language: String!, 
  dialect: String, 
  description: String
}

type Mutation {
  MergeLesson(lessonInput: MergeLessonInput!): Lesson! @cypher(statement:"""
   MERGE (lesson:Lesson {name: $lessonInput.name, language: $lessonInput.language})
   SET lesson = $lessonInput
   RETURN lesson
  """)
}