The GRAND dilemma
a guide to setting up the GRAND stack with Typescript.
Background
I ventured out to build a social media grapher using Neo4J and GraphQL. This is when I came across the GRAND stack. Getting started with Neo4J was fairly simple, but making Neo4j work with React, GraphQL and Typescript was a different story.
I hope this article helps those interested in the GRAND stack and makes their setup process a little bit easier. I have a fully working project on my Github (here) that you’re welcome to look at for reference.
Technologies
- GraphQL + Apollo
- Express JS
- React JS
- Neo4j
- Neovis (neo4J client visualizer)
- Typescript
- Webpack
Typescript Configuration
The most challenging part of setting up the server, was the limited documentation on using GRAND with Typescript. Especially the web-pack settings. These are my final settings:
as you can see, I have some environment variables that I’m using in the front end for Neo4J. You’ll also need a tsconfig file.
The settings above are a combination of trial and error, and using what other people have done in the past, but they cover all the bases.
Server Configuration
For my server, I used Apollo server, GraphQL, Express and Neo4J driver. There are countless tutorials on how to set up GraphQL with Neo4j but I found two libraries to be the most useful.
- neo4j-graphql-js
- apollo-server-express
Here’s why, I wanted to create a basic model that had tweets and users. Users would be connected to tweets and vice versa. By using makeAugmentedSchema from neo4j-graphql-js, I was able to autogenerate resolvers on my GraphQL schema.
By defining my schema like this, I had access to queries and mutations like:
- Fetch users
- Fetch tweets
- Add user to tweets
- Add tweet to users
- Get user tweets
All of this without writing a single additional resolver. This made the relationship between GraphQL and Neo4j much more interesting to me. The rest of the process is pretty straight forward if you have used Apollo before.
React
We’ve covered the G, the A and the ND (Neo4J Database) of GRAND, but we can’t forget about the R. Which is after all, all that matters to our users. To visualize my Neo4j backend, I used a library called Neovis, which of course was not optimized for use with React.
But before I could show something, I had to create some data. I made a simple form that an user can enter their name in, as well as a tweet. I would then make a mutation that would store that data in my Neo4j database.
Because of those ‘magic’ methods created by our backend library, we can easily create users and tweets and assign them to each other. Here’s an example of a mutation I used to create a new tweet and assign it to a user.
Both CreateTweet and AddUserTweets were autogenerated for me by our Neo4j-GraphQL libraries.
Summary
The biggest challenge was to setup all of this AND use Typescript with it. I love the stack and hope to use it again in the future. The potential is immense, even for a small project like mine, I can’t think of another stack that combines graph databases and queries as well as the GRAND stack does.
As I mentioned earlier, I have a fully working example on my Github. Thank you for reading and happy coding!