A progressive Node.js framework for building efficient and scalable server-side applications.
This project is a Hotel Reservation System API built using NestJS, GraphQL, and Mongoose. It provides functionality for managing hotel reservations, including creating, retrieving, and cancelling reservations. It also supports JWT-based authentication and error handling.
$ pnpm install
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
- JWT-based authentication
- CRUD operations for reservations
- Pagination and filtering
- DataLoader for efficient data fetching
- GraphQL API
- Error handling and validation
- Node.js (version 14 or higher)
- MongoDB
Create a .env file and configure the following variables
- MONGO_URI=
- JWT_SECRET=
To check complete documentation of the GraphQL endpoints visit API Reference or use
/api-reference
endpoint for Documentation of Project/graphql-root
for graphQL inspection
To get all reservations of the user, you need to follow these steps
- Sign up using query signup
- Sign in and get token to access all protected queries and mutation
- Pass the Authentication token in headers as { Authorization: Bearer <YOUR_TOKEN>}
- Use variables that need to pass to the query.
- Below are the examples.
query Query($userInput: LoginUserDto!) {
signIn(userInput: $userInput) {
accessToken
}
}
/* Variables */
{
"userInput": {
"email": "test@test.com",
"password": "tester"
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
mutation Mutation($userInput: CreateUserDto!) {
signUp(userInput: $userInput) {
id
name
email
phoneNumber
}
}
/* Variables */
{
"userInput": {
"email": "test5@test.com",
"name": "Test 5 User",
"password": "tester",
"phoneNumber": "1-212-456-7890"
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
query GetHotels {
getHotels {
_id
name
location {
lat
long
}
rating
baseAmount
taxAmount
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
mutation CreateReservation($createReservationDto: CreateReservationDto!) {
createReservation(createReservationDto: $createReservationDto) {
arrivalDate
departureDate
_id
hotelId {
_id
name
}
userId {
_id
name
}
amount
status
}
}
/* Variables */
{
"createReservationDto": {
"arrivalDate": "2024-06-28T08:41:41.143Z",
"departureDate": "2024-06-29T08:41:41.143Z",
"hotelId": "666d7ec4319cadc293131d76"
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
query Query($paginationDto: PaginationDto!) {
getReservations(paginationDto: $paginationDto) {
edges {
cursor
node {
_id
amount
arrivalDate
departureDate
status
userId {
name
_id
}
hotelId {
_id
name
}
}
}
}
}
/* Variables */
{
"paginationDto": {
"cursor": null,
"limit": 20
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
query GetReservation($id: String!) {
getReservation(_id: $id) {
arrivalDate
departureDate
_id
hotelId {
_id
name
}
userId {
_id
name
}
amount
status
}
}
/* Variables */
{
"id": "66729c85228a9875dce161d0"
}
/* Headers */
{ Authorization: Bearer <accessToken> }
mutation Mutation($id: String!) {
cancelReservation(_id: $id)
}
/* Variables */
{
"id": "666da4096b3835179380424d"
}
/* Headers */
{ Authorization: Bearer <accessToken> }
mutation UpdateReservation($id: String!, $updateReservationDto: UpdateReservationDto!) {
updateReservation(_id: $id, updateReservationDto: $updateReservationDto) {
arrivalDate
departureDate
_id
hotelId {
_id
name
}
userId {
_id
name
}
amount
status
}
}
/* Variables */
{
"updateReservationDto": {
"arrivalDate": "2024-07-10T08:41:41.143Z",
"departureDate": "2024-07-11T08:41:41.143Z",
"amount": 1200
},
"id": "66729c85228a9875dce161d0"
}
/* Headers */
{ Authorization: Bearer <accessToken> }
query GuestSummary {
guestSummary {
guestId
upcomingStaysCount
upComingTotalAmount
upComingTotalNights
pastStaysCount
pastTotalAmount
pastTotalNights
cancelledStaysCount
totalStaysAmount
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
query GetStaysInRange($getStaysArgs: GetStaysDto!) {
getStaysInRange(getStaysArgs: $getStaysArgs) {
nextCursor
edges {
cursor
node {
arrivalDate
departureDate
_id
hotelId {
_id
name
}
userId {
_id
name
}
amount
status
}
}
}
}
/* Variables */
{
"getStaysArgs": {
"cursor": null,
"limit": 20,
"startDate": "2024-05-10T08:41:41.143Z",
"endDate": "2024-07-10T08:41:41.143Z"
}
}
/* Headers */
{ Authorization: Bearer <accessToken> }
To run complete application here is the CodeSanbox URL
Errors are handled using a custom GraphQL exception filter. Errors are serialized in a consistent format for better readability.
To run the unit tests, use the following command:
$ npm run test
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Nest is MIT licensed.