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.
- 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
- Docker and Docker Compose
- Node.js (for local development)
- npm or yarn
The project uses Docker Compose to orchestrate the following services:
-
NestJS Application:
- Main application container
- Auto-reloading in development mode
- Exposed on port 3000
-
MongoDB:
- Version 7.0
- Persistent data storage
- Exposed on port 27017
- No authentication required (development mode)
-
Mongo Express:
- Web-based MongoDB admin interface
- Accessible on port 8081
-
Clone the repository:
git clone <repository-url> cd nestjs-graphql-setup
-
Create a
.env
file (optional):NODE_ENV=development PORT=3000 GRAPHQL_SCHEMA_FILE=schema.gql
-
Start the services:
docker-compose up
- GraphQL API and Apollo Sandbox: http://localhost:3000/graphql
- Mongo Express: http://localhost:8081
The application is configured for development with hot-reload. Any changes to the source code will automatically trigger a rebuild of the application.
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
-
Query: hello
- Simple hello world query
- Returns a greeting message
-
Query: cats
- Fetches all cats from the database
- Returns an array of cats with their properties
-
Mutation: createCat
- Creates a new cat in the database
- Input fields:
- name: string
- age: number
- breed: string
src/
- Application source codeschema.gql
- Generated GraphQL schemadocker-compose.yml
- Docker services configurationDockerfile
- NestJS application container configuration
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.