Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


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

    Matt RossMatt Ross

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 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.

    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.

    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.

    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.

    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.