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
- GraphQL
- NestJS server
- MongoDB
- Apollo
- JWT Rolebased Authentication using Passport.js
- Authorization
- User Management
- E2E testing
- Docker Compose
login(email: Email!, password: String!): Auth
register(email: Email!, password: String!): Auth
update(id: String!, user: UpdateUser!): User
delete(email: Email!): User
users: [User!]
user(email: Email!): User
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.
# 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
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=
# 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
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
Here is a list of important commands.
npm run test:e2e
# Docker
docker exec -it nodejs npm run test:e2e
npm run build
npm run start
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.
Gabriel Tanner
This project is licensed under the MIT License - see the LICENSE.md file for details