Create Data in Neo4J Through a GraphQL API

Matt Ross
InstructorMatt Ross

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 2 years ago

We will use automatically generated mutations in a Apollo GraphQL API augmented with neo4j-graphql-js to insert data that we get from SWAPI into Neo4J.

Instructor: [00:01] Let's start with a clean graph. Rather than take the next step and insert some data manually, we're going to leverage the mutations that neo4j-graphql-js has put together for us. We can spin up our API and take a look in the schema tab to see what's available to us.

[00:16] In here, we'll find a set of create node type, delete node type, add relationship, and remove relationship mutations. Let's take a look at fetching some data from Swappy directly and programmatically creating these nodes and relationships through our API's mutations.

[00:32] We'll use Axios to make res requests against Swappy and GraphQL requests to make GraphQL queries and mutations against our API. Let's start by fetching the first of a list of people from Swappy. In the same way, let's fetch the person's home world and their first film and species. Can a person have more than one species?

[00:58] Great. We've got all the data we need. Let's take a look at our schema tab and see what we need to fulfill or create node type mutations. It looks like all we'll need is the name or title properties. Let's write these using GraphQL request. The request function accepts a GraphQL endpoint, a query or mutation, and some optional input params.

[01:25] Let's apply the data we got from Swappy to these functions in main, start our API, and run the code to execute these mutations. It looks like these were all created successfully. Let's go back to the GraphQL playground and see what we can get back.

[01:47] This is a good start, but still lacking are the relationships between these nodes. Let's take a look at the schema tab to see what we need to fulfill our add relationship mutations. It looks like these need a from and to that are defined in the input types that were generated for us. These input types need a name or a title property.

[02:10] Let's add some GraphQL request functions to add some relationships. We'll also comment out our earlier create node calls to avoid creating these nodes again. These functions are ripe for abstraction, but we'll just call that a take-home exercise. Now, we can call our mutations as our results and see what we get back.

[02:37] Fantastic. It looks like all five relationships were created. We can even see the statements that were executed against Neo4j in our API output. Let's go back to the playground and see if we can get back all of our nested data.

[02:53] Amazing. There's one huge caveat in these mutations, relating to the vocabulary in Neo4j's Cypher query language. As we can see in our API output, these mutations all use the create clause. Executing a single create statement multiple times will result in the same node or relationship being created over and over.

[03:17] This is most definitely not what we want. We'll need to learn how to create our own custom mutations to support the merge clause. First though, we'll need to learn how to use the Cypher directive in our schemas.

Isaac Weber
Isaac Weber
~ 3 years ago

How would you write custom resolvers for specific use cases. For instance if you want to hit an XML Api and need to covert it to JSON to be able to map it to a graphql schema?