This project is a simple example of how to work with Neo4j within a Docker container.
All you need to have installed on your system to run this example is Docker.
If you do not have Docker installed on your development system, you can download and install the freely available Docker Desktop.
IMPORTANT: Before starting this project, please be sure to copy nextjs/.env.sample
to nextjs/.env.local
To spin up your Dockerized Neo4j and Next.js project:
# Use the existing Docker images on your system
$ npm run dev
# OR #
# Force a clean build to ensure your instances are using the latest code
$ npm run dev:clean
Once you have started your Dockerized project, you can access the Neo4j Browser at http://localhost:7474/browser/ - using the following credentials:
- Username:
neo4j
- Password:
letmein
Once you've logged in, you are free to explore your Dockerized Neo4j instance.
If you would like to have your database load a pre-defined series of Cypher commands - such as the example at neo4j/v4.x.x/__seed__/db.cypher
- simply uncomment the following in neo4j/v4.x.x/Dockerfile
:
# neo4j/v4.x.x/Dockerfile
...
# COPY ./__seed__/*.cypher /var/lib/neo4j/import/
...
This will ensure that your Cypher file(s) are copied and then processed by the neo4j/v4.x.x/wrapper.sh
script when building your Neo4j instance.
An example Next.js application has been created so that we can use the Neo4j GraphQL Library to create our GraphQL API.
Once you have started your Dockerized project, you can access the Next.js GraphQL API at http://localhost:3000/api/graphql
Let's use the ping
query that we have defined in our GraphQL schema:
{
ping {
message
timestamp
}
}
You should see a response similar to:
{
"data": {
"ping": {
"message": "Pong",
"timestamp": "2021-09-05T02:23:55.000Z"
}
}
}
To create a new User
using our GraphQL API, we will use the createUsers
mutation that was automatically created for us with the Neo4j GraphQL Library:
mutation {
createUsers(input: { username: "therobbrennan" }) {
users {
username
created
}
}
}
You should see a response similar to:
{
"data": {
"createUsers": {
"users": [
{
"username": "therobbrennan",
"created": "2021-09-05T02:24:54.078Z"
}
]
}
}
}
IMPORTANT: This mutation will allow you to create duplicate users if you have not defined the appropriate constraint within your Neo4j database!
If you would like to prevent duplicate User
nodes from being created with the same username
, for example, you can log in to the Neo4j Browser at http://localhost:7474/browser/ and execute the following Cypher command:
CREATE CONSTRAINT ON (node:User) ASSERT (node.username) IS UNIQUE;
In this example, let's count how many User
nodes exist in our Neo4j database - and see each user's username
and created
details:
{
usersCount
users {
username
created
}
}
You should see a response similar to:
{
"data": {
"usersCount": 1,
"users": [
{
"username": "therobbrennan",
"created": "2021-09-05T02:24:54.078Z"
}
]
}
}
An example Next.js application with Tailwind CSS has been created so that we can use the Neo4j GraphQL Library to create our GraphQL API.
Once you have started your Dockerized project, you can access the Next.js GraphQL API at http://localhost:3001/api/graphql
Let's use the ping
query that we have defined in our GraphQL schema:
{
ping {
message
timestamp
}
}
You should see a response similar to:
{
"data": {
"ping": {
"message": "Pong",
"timestamp": "2021-09-05T02:23:55.000Z"
}
}
}
To create a new User
using our GraphQL API, we will use the createUsers
mutation that was automatically created for us with the Neo4j GraphQL Library:
mutation {
createUsers(input: { username: "therobbrennan" }) {
users {
username
created
}
}
}
You should see a response similar to:
{
"data": {
"createUsers": {
"users": [
{
"username": "therobbrennan",
"created": "2021-09-05T02:24:54.078Z"
}
]
}
}
}
IMPORTANT: This mutation will allow you to create duplicate users if you have not defined the appropriate constraint within your Neo4j database!
If you would like to prevent duplicate User
nodes from being created with the same username
, for example, you can log in to the Neo4j Browser at http://localhost:7474/browser/ and execute the following Cypher command:
CREATE CONSTRAINT ON (node:User) ASSERT (node.username) IS UNIQUE;
In this example, let's count how many User
nodes exist in our Neo4j database - and see each user's username
and created
details:
{
usersCount
users {
username
created
}
}
You should see a response similar to:
{
"data": {
"usersCount": 1,
"users": [
{
"username": "therobbrennan",
"created": "2021-09-05T02:24:54.078Z"
}
]
}
}
An example Next.js application with NextAuth.js has been created so that we can use the Neo4j GraphQL Library to create our GraphQL API.
-
Some auth providers (e.g. twitter) don't work with a localhost callback url
If using twitter, set up a .local domain e.g.http://nextjs-with-next-auth.local
A cross platform guide for this is at: Using an /etc/hosts file for custom domains during development -
Create apps with your providers. Follow the GitHub guide, for example, on the NextAuth.js docs. This demo has been made with GitHub and Twitter in mind, but you can adapt the code to suit yourself.
-
Complete the env vars for your providers in
./nextjs-with-next-auth/.env.local
-
Start the docker containers as described earlier in this readme.
-
Once you have started your Dockerized project, you can access the Next.js GraphQL API at http://nextjs-with-next-auth.local:3002/api/graphql
-
You should be able to sign in and out using the buttons in the header.
If you are looking for a great overview on setting up a Next.js GraphQL API, I strongly recommend William Lyon's blog post at https://www.lyonwj.com/blog/graphql-server-next-js-neo4j-aura-vercel.