Skip to content

Files

Latest commit

 

History

History
293 lines (250 loc) · 5.94 KB

File metadata and controls

293 lines (250 loc) · 5.94 KB

Library Management System - GraphQL API

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.


Table of Contents


Features

  • 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.

Technologies Used

  • 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.

Setup Instructions

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or cloud instance)
  • Postman (for testing)

Installation

  1. Clone the repository:

    git clone https://github.com/your-username/library-management-graphql.git
    cd library-management-graphql
  2. Install dependencies:

    npm install
  3. 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
  4. Start the server:

    npm start
  5. Access the GraphQL Playground: Open your browser and navigate to:

    http://localhost:3000/graphql
    

API Documentation

Mutations

  1. 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
          }
        }
      }
  2. Login

    • Description: Authenticate a user and return JWT tokens.
    • Request:
      mutation {
        loginMutation(
          email: "john@example.com",
          password: "password123"
        ) {
          statusCode
          message
          token
          refreshToken
        }
      }
  3. 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
        }
      }
  4. Borrow Book

    • Description: Borrow a book for 2 days.
    • Request:
      mutation {
        borrowBook(
          userId: "userId",
          bookId: "bookId"
        ) {
          _id
          userId
          bookId
          borrowedAt
          dueDate
        }
      }
  5. Delete User

    • Description: Delete a user (authenticated users only).
    • Request:
      mutation {
        deleteUser(id: "userId") {
          _id
          name
          email
        }
      }
  6. Mark Book as Available

    • Description: Mark a borrowed book as available again.
    • Request:
      mutation {
        markBookAvailable(id: "bookId") {
          _id
          title
          availableCopies
        }
      }

Queries

  1. Retrieve All Books

    • Description: Fetch all books in the library.
    • Request:
      query {
        getBooks {
          _id
          title
          author
          genre
          availableCopies
        }
      }
  2. Retrieve Book by ID

    • Description: Fetch a book by its ID.
    • Request:
      query {
        getBookById(id: "bookId") {
          _id
          title
          author
          publishedYear
          genre
        }
      }
  3. Fetch Libraries with Books

    • Description: Fetch libraries along with the books they contain.
    • Request:
      query {
        getLibraries {
          _id
          name
          location
          books {
            _id
            title
            author
          }
        }
      }
  4. Retrieve Overdue Borrowed Books

    • Description: Fetch overdue borrowed books that have not been returned.
    • Request:
      query {
        getOverdueBooks {
          _id
          userId
          bookId
          dueDate
        }
      }

Validation

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.

Error Handling

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
}