Step-by-Step Guide: Set up a Code-First GraphQL Node Server

Spin up a great server stack in 15 minutes

Jeremy Tong
2 min readNov 29, 2021

Introduction

If you’ve read my first article on GraphQL Nexus, you know we have a strong preference at Knit for building GraphQL servers with Nexus and Prisma. One of the benefits of this technology is the speed with which we can spin up a server.

This is a guide to how we like to set up a server using the latest versions of Nexus and Prisma. You will build a Node.js server using Express, Apollo Server, Nexus, Prisma, and TypeScript with ESLint, Prettier, and husky to help standardize your environment.

TL;DR

We prefer to start Nexus servers for new projects from scratch to catch any updates, but if you want to jump right in, our nexus-server-boilerplate includes all the additions listed at the end of the article.

Step 1: Initialize Project with Dependencies

npm init
npm install @prisma/client apollo-server-express dotenv express graphql@15 graphql-middleware graphql-scalars nexus
npm install -D @types/node @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier husky lint-staged prettier prisma ts-node ts-node-dev typescript

Note: Nexus is only compatible with graphql@15

Add the following tsconfig.json file to your project root.

Add the following .eslintrc file to your project root. Optionally add a .prettierrc file as well.

Lastly, you can add the following scripts to your package.json file.

Step 2: Initialize PrismaJS ORM files

npx prisma init

Recommended:

Change the datasource in the schema.prisma file to “postgres”.

Setup a postgresql database using docker.

docker run --name 'postgres' --detach --publish 5432:5432 -e POSTGRES_PASSWORD=postgres postgres

Add a .env file at the root of the project with DATABASE_URL=<your database URL>.

Step 3: Add Generic Nexus Types

Create /src/nexusTypes/index.ts. Add generics.ts to /nexusTypes and export it from index.ts.

https://gist.github.com/jwtong/60d01a02919067a5a731bf19ac77c3b4

Step 4: Add Server Files

Add /src/server.ts.

https://gist.github.com/jwtong/dfbe2cbe243333ad47150be02fc363a7

Add /src/schema.ts.

https://gist.github.com/jwtong/dfbe2cbe243333ad47150be02fc363a7

Post Setup:

Now that you’ve installed all the base files for the project, you can update schema.prisma with your database architecture and write types in the /nexusTypes folder for each of your models.

Useful Additions:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jeremy Tong
Jeremy Tong

Written by Jeremy Tong

Startup-Focused Software Developer

Responses (1)

Write a response