This is a simple GraphQL API for managing books and authors, built with Node.js, Express, MongoDB, and GraphQL.
This project allows you to create, read, and manage books and authors. Each book is linked to an author, and you can query data efficiently using GraphQL.
- Add new authors and books.
- Query books and authors.
- Retrieve all books or authors.
- Get detailed information about a single book or author, including relational data.
- Node.js
- Express
- MongoDB (with Mongoose)
- GraphQL
- Clone the repository:
git clone https://github.com/PJBalogun/graphql-book-api
cd graphql-books-api- Install dependencies:
npm install- Create a
.envfile and add your MongoDB connection string:
MONGODB_URI=<your_mongodb_uri>
- Run the development server:
npm run devThe server will be running at http://localhost:4000/graphql.
.
├── models
│ ├── author.js
│ └── book.js
├── schema
│ └── schema.js
├── app.js
├── package.json
├── .env
└── README.md
BookType
- id
- name
- genre
- author (relational field)
AuthorType
- id
- name
- age
- books (list of books by this author)
Get all books:
{
books {
name
genre
author {
name
age
}
}
}Get a book by ID:
{
book(id: "123") {
name
genre
author {
name
}
}
}Add a new author:
mutation {
addAuthor(name: "J.K. Rowling", age: 55) {
id
name
}
}Add a new book:
mutation {
addBook(name: "Harry Potter", genre: "Fantasy", authorId: "123") {
id
name
}
}This project was created following the Net Ninja GraphQL Crash Course.
This project is open-source and available for educational purposes!
🔥 Happy Coding!