Create Custom GraphQL Mutations with the @cypher Directive in neo4j-graphql-js

Matt Ross
InstructorMatt Ross

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 2 years ago

We will use our own custom mutations in an Apollo GraphQL API to update or create data that we scrape from SWAPI into Neo4J with. We will use the @cypher directive to use custom merge statements in these mutations.

Instructor: [00:01] Our scraper is currently executing mutations using the add relationship mutations provided by neo4j-graphql-js. Because these use the create clause under the hood, applying the same mutation multiple times will result in identical nodes or relationships being created.

[00:20] The Cypher directive can help us solve this problem by allowing us to define custom Cypher queries in our mutations. For instance, ones using the merge clause. We can add some mutations to our schema for node creation using this Cypher directive.

[00:41] If we were interested in doing a little more work, we could define a more robust input type and create a more complex Cypher statement. Let's keep our implementation as simple as possible for now though.

[00:57] We can run the node creation section of the scraper we used before and still expect to have only one instance of each node. The relationship mutations can be overridden in the same way, unlike the generated relationship mutations. We'll just return the from part of each relationship to keep our Cypher statement simple.

[01:28] Because the output data type is changing, we'll need to update the scraper script to return the correct properties. We can run our scraper as many times as we'd like and still have an intact and coherent graph after every run.