This project is a Laravel GraphQL API for retrieving books from a database. It offers a variety of queries and mutations for adding, searching, updating and deleting the books from the database.
- Clone the repository and install dependencies:
git clone https://github.com/mourabitiy/laravel-graphql-book-library
cd laravel-graphql-book-library
composer install
- Generate an app key
php artisan key:generate
- Set up your database credentials in the .env file
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=[YOUR_DB_NAME] DB_USERNAME=[YOUR_DB_USERNAME] DB_PASSWORD=[YOUR_DB_PASSWORD]
- Run the Server
php artisan serve
- Run the database migrations and populate it using faker
php artisan migrate:fresh --seed
- To try out the endpoints, you can use a GraphQL client such as Postman, Insomnia or GraphiQL.
Open the client and set the endpoint to http://127.0.0.1:8000/booksApi
.
Try out some of the available queries and mutations. For example, to Register
query Register($name: String!, $email: String!, $password: String!) { Register(name: $name, email: $email, password: $password) { id name email } }
GRAPHQL VARIABLES
{ "name":"xxxxxxx", "email": "[email protected]", "password": "xxxxxxxxx" }
query Login($email: String, $password: String) { Login(email: $email, password: $password) { id name accessToken } }
{ books { id title author language category page_count publisher isbn } }
{ book(id: 157) { page_count publisher isbn } }
{ books(author: "Daniel") { id title author language category page_count publisher isbn } }
Same as the Author, just change the Parameter to : isbn, language, title, category.
mutation{ createBook( title: "The Book of the Dead", author: "John Doe", language: "English", category: "Horror", page_count: 200, publisher: "John Doe", isbn: "1234567890" ){ title author language category page_count publisher isbn } }
mutation{ updateBook( id: 55778, title: "The Book of the Dead", description: "This is a book about the dead", author: "John The Writer", language: "En", category: "Horror", page_count: 200, publisher: "John Doe", isbn: "1234567890" ){ id title author language category page_count publisher isbn } }
mutation{ deleteBook( id: 55778 ){ id title author language category page_count publisher isbn } }