Skip to content

πŸš€ A robust and scalable backend starter kit built with NestJS, GraphQL, Prisma, and PostgreSQL. Features Docker support, JWT authentication, and comprehensive testing.

License

Notifications You must be signed in to change notification settings

hmake98/nestjs-graphql-starter

Repository files navigation

NestJS GraphQL Boilerplate

Tests

NestJS GraphQL Prisma PostgreSQL Docker Jest

A robust backend starter built with NestJS, GraphQL, Prisma, and PostgreSQL. This boilerplate includes authentication, Docker support, and comprehensive testing setup.

🌟 Features

  • NestJS: A powerful Node.js framework for scalable server-side applications
  • GraphQL & Apollo Server: Fully-featured GraphQL API
  • Prisma: Modern ORM for type-safe database access
  • PostgreSQL: Reliable, open-source relational database
  • Authentication: Secure user authentication system
  • Docker: Containerization for easy deployment and development
  • Testing: Comprehensive test suite using Jest
  • Yarn: Fast and secure dependency management

πŸ›  Prerequisites

Ensure you have the following installed:

  • Node.js (v20.x or later)
  • Yarn (v1.22.x or later)
  • Docker and Docker Compose

πŸš€ Quick Start with Docker

  1. Clone the repository

    git clone https://github.com/yourusername/your-repo-name.git
    cd your-repo-name
  2. Set up environment variables

    cp example.env .env

    Update the .env file with your configuration

  3. Start the application

    docker-compose up -d
  4. Access the application

    • Main: http://localhost:3001
    • GraphQL Playground: http://localhost:3001/graphql

πŸ–₯ Manual Installation

If you prefer to run the application without Docker:

  1. Follow steps 1-2 from the Quick Start guide

  2. Install dependencies

    yarn install
  3. Set up the database

    • Ensure PostgreSQL is running
    • Run migrations:
      yarn migrate
  4. Start the application

    yarn dev

βš™οΈ Configuration

The application uses environment variables for configuration. Copy the example.env file to .env and update the values accordingly. Here's an explanation of the key environment variables:

Variable Description Example Value
NODE_ENV Application environment local
APP_CORS_ORIGIN CORS origin setting *
HTTP_HOST HTTP server host 0.0.0.0
HTTP_PORT HTTP server port 3001
GRAPHQL_PLAYGROUND Enable GraphQL Playground true
DATABASE_URL PostgreSQL connection string postgresql://user:password@localhost:5432/dbname
AUTH_ACCESS_TOKEN_SECRET Secret for JWT token generation my-access-token-secret
AUTH_ACCESS_TOKEN_EXP JWT token expiration time 1d
AWS_REGION AWS region for services ap-south-1
AWS_LOCAL_PROFILE_NAME AWS local profile name default
AWS_ROLE_ARN AWS IAM role ARN arn:aws:iam::123456789012:role/YourRoleName
AWS_S3_PRESIGN_LINK_EXPIRES S3 presigned URL expiration (seconds) 1800
AWS_S3_BUCKET S3 bucket name your-bucket-name

Ensure all variables are properly set before running the application.

πŸ” Authentication

This boilerplate includes a JWT-based authentication system:

  1. Register a user or use the default admin account
  2. Obtain a JWT token by logging in
  3. Include the token with Bearer in the Authorization header of your requests

JWT configuration:

  • Secret key: Set in AUTH_ACCESS_TOKEN_SECRET
  • Token expiration: Set in AUTH_ACCESS_TOKEN_EXP

πŸ§ͺ Testing

Run the test suite:

yarn test

πŸ—ƒ Database Management

Prisma CLI commands for database management:

Command Description
yarn generate Generate Prisma client
yarn migrate:prod Apply migrations

Ensure DATABASE_URL is correctly set in your .env file.

πŸ“Š GraphQL Development

  • Schema is auto-generated from your resolvers
  • GraphQL Playground is enabled by default in development (GRAPHQL_PLAYGROUND=true)
  • Access GraphQL Playground at http://localhost:3001/graphql
  • Disable GraphQL Playground in production for security

🐳 Docker in Development

For a consistent development environment with hot-reloading:

docker-compose -f docker-compose.dev.yml up

☁️ AWS Configuration

This project uses AWS services. Ensure proper configuration:

  1. Set AWS_REGION to your desired AWS region
  2. Configure AWS_LOCAL_PROFILE_NAME with your AWS CLI profile name
  3. Set AWS_ROLE_ARN to the appropriate IAM role ARN
  4. For S3 operations, set AWS_S3_BUCKET and AWS_S3_PRESIGN_LINK_EXPIRES

🚒 Deployment

  1. Build the Docker image

    docker build -t your-app-name .
  2. Push the image to your container registry

  3. On your server, run:

    docker run -d -p 3001:3001 --env-file .env your-app-name

🀝 Contributing

Contributions are welcome! Please read our contributing guidelines and code of conduct.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

πŸš€ A robust and scalable backend starter kit built with NestJS, GraphQL, Prisma, and PostgreSQL. Features Docker support, JWT authentication, and comprehensive testing.

Topics

Resources

License

Stars

Watchers

Forks