Fueled / nodejs-graphql-template

Opinionated starter template for NodeJS based GraphQL Server with prisma used at Fueled.
11 stars 3 forks source link
apollo-server eslint jest nodejs prisma typescript

Fueled NodeJS GraphQL starter template

This repo contains the starter template for a NodeJS + TypeScript based GraphQL project.

Requirements

Features

Husky is used for pre-commit hooks. It gets installed automatically via npm i as we have prepare lifecycle hook set. To avoid executing pre-commit scripts - append --no-verify flag to git commit.

Get Started

The following will install all necessary packages and run the app under development mode which uses nodemon for reloading the build when changes are made.

npm install
npm run start:dev

Docker Setup

A Dockerfile is available to run the backend services in a container exposed through ports. The Docker container includes the hot reloading Node application, PostgresSQL database, and Redis for caching.

The following services are available currently:

app        // Node application
redis      // Redis server
postgres   // PostgreSQL database

To build and run the Docker container, ensure you have the Docker CLI available, and optionally Docker Desktop.

To build the Docker container:

docker-compose build

To start the Docker container:

docker-compose up [-d]

To stop the Docker container:

docker-compose down

To run a specific container only:

docker-compose up [-d] <container_name>

Authentication

This repository provides basic interfaces & services to allow for a JWT based authentication mechanism to be used.

JwtService - provides methods for signing & verifying a JWT token, along with others.

Authenticable interface - to be implemented by the auth subject (e.g. user model). Current properties (username) are placeholders and are to be replaced with actual properties. When using the Authenticable, please replace the generic type in graphql-context.ts with the actualy implementation for better type hinting.

Testing

This repository has npm commands & examples ready for:

Unit tests

Unit tests should be used to test smaller fragments of code, for example, services. Database calls & HTTP calls are expcted to be mocked within unit tests. Any file within test/ directory with a suffix of .spec.ts will be treated as a unit test file.

E2E tests

E2E tests should be used to test API endpoints. It is expected to use actual database functionality instead of mocks. External 3rd party HTTP requests should be mocked. Any file within test/ directory with a suffix of .e2e-spec.ts will be treated as an E2E test file.

Code formatting & quality

This starter template uses ESLint along side Prettier, with separated responsibilities:

ESLint

ESLint is a code linter which mainly helps catch quickly minor code quality and style issues.

Like most linters, ESLint has a wide set of configurable rules as well as support for custom rule sets. All rules are configured through .eslintrc configuration file. In this starter template ESLint rules are definied specifically around code quality, rather than formatting.

Running ESLint

npm run lint      // ESLint checks
npm run lint:fix  // ESLint checks + attemt to fix errors
npm run lint:all  // ESLint checks + attempt to fix errors & warnings

VS Code extensions

If you are interested in seeing ESLint feedback as soon as possible, I strongly recommend the VS Code ESLint extension.

Prettier

Prettier is a code formatting tool which helps enforce a set of standardized formatting rules across all of our NodeJS projects. It's not able to enforce specific rules for code quality (especially with TypeScript), hence that responsibility is left to ESLint.

Running Prettier

npm run prettier      // Code formatting checks only
npm run prettier:fix  // Code formatting fixes

VS Code extensions

The following plugin allows you to set Prettier as default code formatter as well as apply Prettier code formating upon file save - VS Code Prettier extension.

Helpful VS Code extensions