Skip to content

Badaueba/nestjs-graphql-setup

Repository files navigation

NestJS GraphQL MongoDB Demo

This is a demonstration project showcasing the integration of NestJS with GraphQL and MongoDB. It serves as a practical learning resource for developers interested in building modern Node.js applications with these technologies.

Features Demonstrated

  • NestJS Framework: Modern Node.js framework with TypeScript support
  • GraphQL Integration: Using @nestjs/graphql with Apollo Server
  • Apollo Sandbox: Interactive GraphQL IDE for testing queries
  • MongoDB Database: NoSQL database integration using Mongoose
  • Docker Infrastructure: Containerized development environment
  • Development Tools:
    • Apollo Sandbox for API testing and exploration
    • Mongo Express for database management
    • Hot-reload development setup

Prerequisites

  • Docker and Docker Compose
  • Node.js (for local development)
  • npm or yarn

Infrastructure Components

The project uses Docker Compose to orchestrate the following services:

  1. NestJS Application:

    • Main application container
    • Auto-reloading in development mode
    • Exposed on port 3000
  2. MongoDB:

    • Version 7.0
    • Persistent data storage
    • Exposed on port 27017
    • No authentication required (development mode)
  3. Mongo Express:

    • Web-based MongoDB admin interface
    • Accessible on port 8081

Getting Started

  1. Clone the repository:

    git clone <repository-url>
    cd nestjs-graphql-setup
  2. Create a .env file (optional):

    NODE_ENV=development
    PORT=3000
    GRAPHQL_SCHEMA_FILE=schema.gql
  3. Start the services:

    docker-compose up

Accessing Services

Development

The application is configured for development with hot-reload. Any changes to the source code will automatically trigger a rebuild of the application.

Example Implementation: Cats API

This project includes a complete example of a Cats API that demonstrates the integration between NestJS, GraphQL, and MongoDB. The example showcases:

  • GraphQL Schema Definition using TypeScript decorators
  • CRUD operations with MongoDB
  • GraphQL Queries and Mutations
  • Input type validation
  • MongoDB Schema and Model setup

Available Operations

  1. Query: hello

    • Simple hello world query
    • Returns a greeting message
  2. Query: cats

    • Fetches all cats from the database
    • Returns an array of cats with their properties
  3. Mutation: createCat

    • Creates a new cat in the database
    • Input fields:
      • name: string
      • age: number
      • breed: string

Example Screenshots

GraphQL API Example Apollo Sandbox Interface

MongoDB Integration MongoDB Data Storage

API Testing API Testing and Documentation

Project Structure

  • src/ - Application source code
  • schema.gql - Generated GraphQL schema
  • docker-compose.yml - Docker services configuration
  • Dockerfile - NestJS application container configuration

Note

This is a demonstration project intended for learning and practice purposes. It showcases best practices for setting up a NestJS application with GraphQL and MongoDB in a containerized environment. For production deployments, you should enable proper authentication and security measures.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published