/nestjs-typescript-graphql-api

A containerized boilerplate for a TypeScript-first NestJS backend with code-first GraphQL, JWT (Passport) authentication, MongoDB (MikroORM), and concise (Unit & E2E) tests.

Primary LanguageTypeScriptMIT LicenseMIT

NestJS (TypeScript) GraphQL API Boilerplate

CI codecov

A containerized boilerplate for a TypeScript-first NestJS backend with code-first GraphQL, JWT (Passport) authentication, MongoDB (MikroORM), and concise (Unit & E2E) tests.

Features

  • Code-first GraphQL, with Apollo Sandbox integration (GraphQL IDE)
  • Persistence: MikroORM (MongoDB driver)
  • Testing (Vitest): AAA, GraphQL E2E, Codecov
  • JWT Authentication (Nest/Passport)
  • Architecture: Domain-driven file structure
  • Configuration: Environment variables (dotenv)
  • Strong typing (TypeScript) and validation (class-validator)
  • Containerization: Docker
  • Continuous Integration: GitHub Actions

Requirements

  • Node.js (tested with v18 and newer)
  • MongoDB (tested with Community Server v7)

Setup

  1. Clone the repository.

  2. Provide environment variables in .env.development:

    # copy the example/defaults
    cp .env.example .env.development

    You can change the values in the .env.development file to match your environment. The default MONGO_URI targets a typical locally running MongoDB server instance (127.0.0.1:27017).

  3. Install the project dependencies:

    npm install

Running the app

Development mode (with hot-reload):

npm run start:dev

...or without hot-reload:

npm run start

Production mode:

npm run start:prod

After running the app, you can access the GraphQL sandbox at http://localhost:8008/graphql

Test

Running unit tests

npm run test
npm run test:watch    # watch mode
npm run test:cov      # coverage
npm run test:debug    # debug

Coverage report for unit tests are exported to ./coverage/unit/.

Running End-to-End (E2E) tests

An in-memory MongoDB server is used for the E2E tests, so you don't need to have a MongoDB server running.

npm run test:e2e
npm run test:e2e:cov  # coverage

Coverage report for E2E tests are exported to ./coverage/e2e/.