This repo contains the starter template for a NodeJS + TypeScript based GraphQL project.
- NodeJS v16+
- NPM 7+
- TypeScript
- ExpressJS
- Apollo Server with apollo-server-express integration
- Prisma ORM for database integration
- Jest for testing
- Code formating & linting
- ESLint for code quality standards
- Prettier with eslint-config-prettier for auto-formatting
- EditorConfig
- Husky for pre-commit hooks
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
.
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
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>
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.
This repository has npm commands & examples ready for:
- Unit tests
- E2E 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 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.
This starter template uses ESLint along side Prettier, with separated responsibilities:
- ESLint for code quality standards.
- Prettier for code formatting standards.
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.
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
If you are interested in seeing ESLint feedback as soon as possible, I strongly recommend the VS Code ESLint extension.
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.
npm run prettier // Code formatting checks only
npm run prettier:fix // Code formatting fixes
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.
- Format Code Action - lets you run ESLint & Prettier in a particular order of your preference.