Skip to content

This is a sample project to allow you to quickly spin up and explore a Dockerized Neo4j Graph Database with a standard Next.js application with TypeScript as well as a second Next.js application that uses Tailwind CSS

Notifications You must be signed in to change notification settings

usedandloved/dockerized-neo4j-with-nextjs

 
 

Repository files navigation

Welcome

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.

Getting started

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

Neo4j

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

neo4j/v4.x.x/screenshots/neo4j-browser-login.png

Once you've logged in, you are free to explore your Dockerized Neo4j instance.

neo4j/v4.x.x/screenshots/neo4j-browser-example.png

OPTIONAL: Use Cypher text file(s) to seed your database

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.

Next.js

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

EXAMPLE: Create a query to verify our GraphQL API is online

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"
    }
  }
}

nextjs/screenshots/graphiql-example-01-ping.png

EXAMPLE: Create a new User

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"
        }
      ]
    }
  }
}

nextjs/screenshots/graphiql-example-02-create-user.png

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;

EXAMPLE: Query all of the users in our database

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"
      }
    ]
  }
}

nextjs/screenshots/graphiql-example-03-query-all-users.png

Next.js with Tailwind CSS

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

EXAMPLE: Create a query to verify our GraphQL API is online

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"
    }
  }
}

nextjs/screenshots/graphiql-example-01-ping.png

EXAMPLE: Create a new User

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"
        }
      ]
    }
  }
}

nextjs/screenshots/graphiql-example-02-create-user.png

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;

EXAMPLE: Query all of the users in our database

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"
      }
    ]
  }
}

nextjs/screenshots/graphiql-example-03-query-all-users.png

Next.js with next-auth

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.

  1. 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

  2. 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.

    nextjs-with-next-auth/screenshots/create-github-app.png nextjs-with-next-auth/screenshots/create-twitter-app.png

  3. Complete the env vars for your providers in ./nextjs-with-next-auth/.env.local

  4. Start the docker containers as described earlier in this readme.

  5. 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

  6. You should be able to sign in and out using the buttons in the header.

  7. A list of Users will be shown on the index page. nextjs-with-next-auth/screenshots/index-page.png

  8. The users can be queried at the graphql playground. nextjs-with-next-auth/screenshots/graphiql-example-01-user.png

Resources

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.

About

This is a sample project to allow you to quickly spin up and explore a Dockerized Neo4j Graph Database with a standard Next.js application with TypeScript as well as a second Next.js application that uses Tailwind CSS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 57.5%
  • CSS 19.0%
  • Dockerfile 14.7%
  • Shell 6.7%
  • JavaScript 2.1%