Skip to content

๐Ÿ“š Simple GraphQL Library Management System built with Go and gqlgen

Notifications You must be signed in to change notification settings

thanhhaudev/go-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

57 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Simple GraphQL Library Management System

This project is a Simple Library Management System built with Golang and gqlgen. It provides a GraphQL API for managing books, authors, and borrowers, along with borrowing and returning books.

Features

  • Book Management: Create, update, and retrieve book information.
  • Author Management: Manage author records.
  • Borrower Management: Handle borrower data.
  • Borrowing System: Track book borrowing and returning status.

Database Schema

The system's database schema is as follows:

+--------------------+         +--------------------+        +--------------------+
|      authors       |         |     authors_books  |        |       books        |
+--------------------+         +--------------------+        +--------------------+
| id                 |<------->| author_id          |<------>| id                 |
| name               |         | book_id            |        | title              |
| created_at         |         |                    |        | description        |
| updated_at         |         +--------------------+        | quantity           |
| deleted_at         |                                       | rating             |
+--------------------+                                       | publish_at         |
                                                             | created_at         |
                                                             | updated_at         |
                                                             | deleted_at         |
                                                             +--------------------+
                                                                       |
                                                                       |
                                                                       |
                                                             +---------v---------+
                                                             |   borrower_books   |
+--------------------+                                       +--------------------+
|     borrowers      |                                       | id                 |
+--------------------+                                       | borrower_id        |
| id                 |<------------------------------------->| book_id            |
| name               |                                       | borrowed_at        |
| address            |                                       | returned_at        |
| tel_number         |                                       | due_date           |
| birth_date         |                                       | status             |
| created_at         |                                       | quantity           |
| updated_at         |                                       | created_at         |
| deleted_at         |                                       | updated_at         |
+--------------------+                                       | deleted_at         |
                                                             +--------------------+

Technologies Used

  • Go: Primary programming language for the backend.
  • GORM: Object-Relational Mapping (ORM) library for Go.
  • gqlgen: Library for generating GraphQL server code in Go.

Getting Started

Prerequisites

Requirement Version
Docker Compose Latest

Installation

  1. Clone the repository:

    git clone https://github.com/thanhhaudev/go-graphql.git
    cd go-graphql
  2. Build and start the Docker containers:

    make build
    make run
  3. Migrate the database schema:

    make migrate
  4. Seed the database with sample data:

    make seed

Running the Application

  1. Ensure the Docker containers are running:

    make run && make logs
  2. Access the GraphQL playground at http://localhost:8080/.

  3. To stop the Docker containers:

    make stop

Usage Examples

GraphQL Queries and Mutations

  • Get all Books:

    query {
        books {
            id
            title
            authors {
                id
                name
            }
        }
    }
  • Get a Book by ID:

    query {
        book(id: 1) {
            id
            title
            authors {
                id
                name
            }
        }
    }
  • Get all Authors:

    query {
        authors {
            id
            name
            books {
                id
                title
            }
        }
    }
  • Get all Borrowers:

    query {
        borrowers {
            id
            name
            books {
                id
                title
            }
        }
    }
  • Create a Book:

    mutation {
        createBook(
            input: {title: "Title", authorIds: [1, 2], publishAt: "2024-09-30T07:00:00.000Z", quantity: 100, rating: 4.2}
        ) {
            id
            title
            createdAt
        }
    }
  • Borrow a Book:

    mutation {
        borrowBook(
            input: {bookId: 1, borrowerId: 1, quantity: 5, dueDate: "2024-09-30T07:00:00.000Z"}
        ) {
            name
            books {
                title
            }
        }
    }
  • Return a Book:

    mutation {
        returnBook(input: {bookId: 2, borrowerId: 1}) {
            name
            books {
                id
                title
            }
        }
    }

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and test them.
  4. Submit a pull request.