GraphQL & Neo4j : how to modify a property with a mutation request?

122 Views Asked by At

I have article nodes in a Neo4j database. One of his property is a boolean: "saved: false". The property name of this node is "saved".

I have a React interface where I want users to be able to save articles as favorites. So what I would like to do is change the property "saved" to true when they want to save an article or set to false when they want to cancel the favorite.

This process can be done with a graphql request but I don't know how to write my request to modify a node property.

Example: The user wants to save the article with id = 1, so he clicks on the button "favorite" and the article node property id = 1 and the node property "saved" goes from false to true.

I just want to update a node property, like this I think:

type Mutations {
    saved(article:ID! director:ID!) : ??? (string ?)
      @cypher(statement:"MATCH (a:Article {id: $id, saved:$saved})
                         SET a.saved += true ")
}
schema {
   mutations: Mutations
}

How can I do it ?

1

There are 1 best solutions below

0
Dan Starns On

Id suggest using an optional match and use SET.

OPTIONAL MATCH (a:Article {id:$id, saved:$saved})
SET a.saved = true
WITH a IS NOT NULL AS found
RETURN found

Here I am returning a boolean to represent if the node was matched, thus set. Using the boolean here to reduce the amount of data sent back but you could just as easily return the node.

type Mutations {
    saved(article: ID! director: ID!): Boolean @cypher(statement:"""
      OPTIONAL MATCH (a:Article {id:$id, saved:$saved})
      SET a.saved = true
      WITH a IS NOT NULL AS found
      RETURN found
    """)
}