NestJS GraphQL Server with MongoDB, a powerful and scalable tech stack that offers a robust foundation for building efficient server-side applications.
NestJS provides a modular and type-safe framework, while GraphQL serves as a powerful query language and runtime, providing a single endpoint to reduce network overhead and enable precise data retrieval.
MongoDB, a flexible NoSQL database, ensures seamless integration and efficient data retrieval.
- Install MongoDB and run it locally. Alternatively, you can pull a docker image for MongoDB and run it.
$ docker pull mongo:latest $ docker run -d -p 27017:27017 --name=mongo-dev mongo:latest $ docker ps
- Run the command
$ git clone https://github.com/nc1z/demo-nestjs-graphql-server.git $ cd demo-nestjs-graphql-server/ $ npm run start:dev
- Now you have the endpoint
localhost:3000/graphql
exposed and ready to be queried!
Via GraphQL playground
-
Simply head to
localhost:3000/graphql
and write your queries in the field. For example:Hello world
{ hello }
Create a User
mutation { createUser(input: {name: "Mary Jane", age: 18, email:"mj@gmail.com"}) { id name age email } }
Via Postman
Create a POST request and select the request body as GraphQL
-
Setup NestJs modules with modular / layered NestJs architecture - module, service, repository, dto...
-
Setup Mongoose/MongoDB schemas, models...
-
Remember to use
mongodb//127.0.0.1:27017/your-db-name
for newer nodeJs versions
- Ben Awad for the tutorial
- Michael Guay for debugging
- NestJs documentation for setup guide and debugging