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.
- 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.
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 |
+--------------------+
- Go: Primary programming language for the backend.
- GORM: Object-Relational Mapping (ORM) library for Go.
- gqlgen: Library for generating GraphQL server code in Go.
Requirement | Version |
---|---|
Docker Compose | Latest |
-
Clone the repository:
git clone https://github.com/thanhhaudev/go-graphql.git cd go-graphql
-
Build and start the Docker containers:
make build make run
-
Migrate the database schema:
make migrate
-
Seed the database with sample data:
make seed
-
Ensure the Docker containers are running:
make run && make logs
-
Access the GraphQL playground at
http://localhost:8080/
. -
To stop the Docker containers:
make stop
-
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 } } }
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and test them.
- Submit a pull request.