This project is a Library Management System built using GraphQL, Express, and MongoDB. It provides a set of mutations and queries to manage users, books, libraries, and borrowed books. The API is designed to handle user authentication, book borrowing, and library management.
- User Management: Signup, login, and delete users.
- Book Management: Add, borrow, and mark books as available.
- Library Management: Fetch libraries along with their books.
- Authentication: JWT-based authentication for secure access.
- Validation: Input validation for all mutations and queries.
- Error Handling: Structured error responses for better debugging.
- GraphQL: For building the API.
- Express: For the server.
- MongoDB: For database storage.
- Mongoose: For MongoDB object modeling.
- JWT: For user authentication.
- Joi: For input validation.
- Bcrypt: For password hashing.
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- Postman (for testing)
-
Clone the repository:
git clone https://github.com/your-username/library-management-graphql.git cd library-management-graphql
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in the root directory and add the following:PORT=3000 MONGODB_URI=mongodb://localhost:27017/library JWT_SECRET=your_jwt_secret_key
-
Start the server:
npm start
-
Access the GraphQL Playground: Open your browser and navigate to:
http://localhost:3000/graphql
-
Signup
- Description: Register a new user.
- Request:
mutation { signUpMutation( name: "John Doe", email: "john@example.com", phone: "1234567890", password: "password123" ) { statusCode message user { _id name email phone } } }
-
Login
- Description: Authenticate a user and return JWT tokens.
- Request:
mutation { loginMutation( email: "john@example.com", password: "password123" ) { statusCode message token refreshToken } }
-
Add Book
- Description: Add a new book to the library.
- Request:
mutation { addBook( title: "Sample Book", author: "authorId", publishedYear: 2021, genre: "Fiction", availableCopies: 5 ) { _id title author } }
-
Borrow Book
- Description: Borrow a book for 2 days.
- Request:
mutation { borrowBook( userId: "userId", bookId: "bookId" ) { _id userId bookId borrowedAt dueDate } }
-
Delete User
- Description: Delete a user (authenticated users only).
- Request:
mutation { deleteUser(id: "userId") { _id name email } }
-
Mark Book as Available
- Description: Mark a borrowed book as available again.
- Request:
mutation { markBookAvailable(id: "bookId") { _id title availableCopies } }
-
Retrieve All Books
- Description: Fetch all books in the library.
- Request:
query { getBooks { _id title author genre availableCopies } }
-
Retrieve Book by ID
- Description: Fetch a book by its ID.
- Request:
query { getBookById(id: "bookId") { _id title author publishedYear genre } }
-
Fetch Libraries with Books
- Description: Fetch libraries along with the books they contain.
- Request:
query { getLibraries { _id name location books { _id title author } } }
-
Retrieve Overdue Borrowed Books
- Description: Fetch overdue borrowed books that have not been returned.
- Request:
query { getOverdueBooks { _id userId bookId dueDate } }
All mutations and queries are validated using Joi. The following fields are validated:
- Email: Must be a valid email address.
- Password: Must be at least 8 characters long and contain at least one letter and one number.
- Phone: Must be a 11-digit number.
- Published Year: Must be a valid year.
Errors are returned in a structured format with the following fields:
- message: A descriptive error message.
- statusCode: The HTTP status code (e.g., 400, 404, 500).
- errorCode: A custom error code for easier debugging.
Example:
{
"errors": [
{
"message": "User not found",
"statusCode": 404,
"errorCode": "USER_NOT_FOUND"
}
],
"data": null
}