/node-starter-graphql

A starter project for a backend app, featuring Nodejs, Typescript, Graphql, sequelize-typescript and Postgres as a data store. It is also dockerized.

Primary LanguageTypeScript

A Nodejs sequelize-typescript Postgres Graphql JWT Docker Starter Project

Prerequisites

What's included

  • Apollo Graphql server
  • Authentication with JWT. Uses HTTP read only cookies.
  • Example (User) GraphQL schema and resolvers
  • PostgreSQL for Data Store (Used in a docker container)
  • sequelize-typescript project as ORM
  • Dockerfile and docker-compose file to set up in dev environment
  • Migrations with umzug from sequelize

How to run the first time:

Not the first time? Skip to How to run subsequently

  • Pull this repository
  • copy .env.example to .env. You can change JWT_SECRET to any valid string (if you wish, it's not important on dev)
  • In .env modify ORIGINS to add your Frontend Client URL like this
ORIGINS=https://studio.apollographql.com,[YOUR_URL]
  • Still in .env change SECURE=1 to SECURE= in order to make requests from a http non-secure URL, which your Frontend client may be. (To test with the Graphql Apollo client directly this needs to be equal to 1)
  • cd into the root directory run docker-compose up --build -d

How to use:

  • After the docker-compose command, wait 1 min and navigate to http://localhost:4001/graphql and you can run your graphql queries there to test them directly.
  • If you see the graphql server interface then it means the api should be working, you can now make requests from your client but before that,
  • Enable cookies in Apollo Studio, so visit http://localhost:4001/graphql , go to settings (Gear Icon) and click the cookie switch on: image showing apollo cookies option

How to run subsequently:

  • cd into the root directory run docker-compose up --build -d

To stop running the app:

  • cd into the root directory run docker-compose down

Housekeeping

  • docker system prune -a is useful to run regularly to clean up unused containers and orphan images so they don't hog your memory.

Migrations

There are scripts that handle migration. To generate a migration file, run yarn generate-migration [model-name] [action] For example a real command could look like this: yarn generate-migration order create This file will be created in src/migrations On development, migrations will automatically be run when you run the docker-compose up command in earlier steps.

API Documentation

After running the app, visit the the Graphql Server GUI, Click on Query your Server and on the left, See Documentation

Examples of Queries

  • For example to get a User, for a particular Id, and also handle errors, you have to pass this to the graphql URL
 Query {
   getUser(id: 1) {
    ... on Error {
      message
    }

    ... on User {
      email, name
    }
  }
 }

Gotchas

  • Is something else running on port 4001? We need this port. You can change it of course, just make sure you use the right URLs then when trying to access this app :)
  • Getting Cors Errors? Open Issue or PR