nestjs-graphql-boilerplate
nestjs-graphql-boilerplate copied to clipboard
Nestjs + GraphQL + MongoDB + Passportjs Boilerplate
NestJS + GraphQL + MongoDB + PassportJS Boilerplate
This is an ever-evolving starter kit for NestJS projects with GraphQL and MongoDB.
- Features
- Requirements
- Available Mutations
- Available Queries
- Securing a specific route
- Getting started
- Important commands
- Contribution
Features
- GraphQL
- NestJS server
- MongoDB
- Apollo
- JWT Rolebased Authentication using Passport.js
- Authorization
- User Management
- E2E testing
- Docker Compose
Requirements
Available Mutations
login(email: Email!, password: String!): Auth
register(email: Email!, password: String!): Auth
update(id: String!, user: UpdateUser!): User
delete(email: Email!): User
Available Queries
users: [User!]
user(email: Email!): User
Securing a specific route
You can secure a route using two predefined guards that can check for the authentification state or the user role.
Only for authenticated users:
@UseGuards(GraphqlAuthGuard)
@Resolver()
export class UserResolver {
}
Only for users with specific roles:
@UseGuards(RolesGuard)
@Resolver()
export class UserResolver {
// only Admins can access this function
@Roles("Admin")
@Query(returns => [UserType])
async users() {
return await this.userService.showAll();
}
}
The current version provides no option to create the first admin account automatically and instead is made for creating the first admin manually (Which will be the case for most applications). After creating the first admin you can change the roles by using the update mutation.
Getting started
Installation
# Clone the repository
git clone https://github.com/TannerGabriel/nestjs-graphql-boilerplate.git
# Enter into the directory
cd nestjs-graphql-boilerplate/
# Install the dependencies
npm install
Configuration
The application can be further configured using environment variables. Here is a list of the environment variables and their standard values.
# The host url of the database (default = localhost)
DATABASE_HOST=
# The port the application runs on (default = 3000)
PORT=
Starting the application
# development
npm run start
# watch mode
npm run start:dev
# production mode
npm run start:prod
Visit http://localhost:3000/graphql for the GraphQL playground
Starting using Docker
The application also includes a Docker Compose file, which makes it easier to get your application running.
# Build the image for the application
docker-compose build
# Run the application in detached mode
docker-compose up -d
Important commands
Here is a list of important commands.
Run E2E tests
npm run test:e2e
# Docker
docker exec -it nodejs npm run test:e2e
Build the application
npm run build
Start the application in production
npm run start
Contribution
Anyone is welcome to contribute to this repository. However, if you decide to do so, I would appreciate it if you take a moment and review the guidelines.
Author
Gabriel Tanner
Support me
License
This project is licensed under the MIT License - see the LICENSE.md file for details