/demo-nestjs-graphql-server

Scalable NestJs GraphQL Server with MongoDB

Primary LanguageTypeScript

Nest Logo

📜 NestJs GraphQL Server with MongoDB

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.

How to run

  1. 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
  2. Run the command
    $ git clone https://github.com/nc1z/demo-nestjs-graphql-server.git
    $ cd demo-nestjs-graphql-server/
    $ npm run start:dev
  3. Now you have the endpoint localhost:3000/graphql exposed and ready to be queried!

How to query

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

image

How the project was setup

  1. Setup GraphQL module

  2. Setup GraphQL resolvers

  3. Setup NestJs modules with modular / layered NestJs architecture - module, service, repository, dto...

  4. Setup Mongoose/MongoDB schemas, models...

  5. Setup MongoDB connection

    Remember to use mongodb//127.0.0.1:27017/your-db-name for newer nodeJs versions

Acknowledgement