Skip to content

Application demonstrates GraphQL/Rest API integration for managing book data, with MongoDB, Swagger, and graphical interface.

Notifications You must be signed in to change notification settings

Dyspersja/GraphQL-RestAPI-BookApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL-RestAPI-BookApp

This Node.js application was created during one of the university classes. It demonstrates the integration of both RESTful API and GraphQL functionalities for managing simple Book data stored in MongoDB.

Setup Instructions

Below is step by step instruction on how to run this project on a local machine:

  1. Clone the Repository: Start by cloning this repository to your local machine using the following command:
git clone https://github.com/Dyspersja/GraphQL-RestAPI-BookApp.git

Alternatively, if you don't have Git CLI installed on your computer, you can download the repository by navigating to the <> Code tab and selecting Download ZIP.

  1. Install Dependencies: Navigate to the project directory and install necessary dependencies using npm nstall:
cd GraphQL-RestAPI-BookApp
npm install

NOTE! Please ensure that Node.js is installed on your computer. The project will not be able to run without it.

  1. Start MongoDB Server: Application comunicates with MongoDB to store the Book data and is needed to run this application.

Using Docker (Optional): If you have Docker installed, you can use the provided scripts to set up and manage a MongoDB container. Run the following commands:

npm run docker:mongo:build   // Build the MongoDB Docker image
npm run docker:mongo:start   // Start the MongoDB container

After launching the Docker container, a MongoDB data directory named mongo_data/ will be automatically generated within the project directory. Later database files will be stored within this directory.

Without Docker: If you don't have Docker installed or prefer to run MongoDB directly, Start fresh MongoDB server on default MongoDB port 27017.

  1. Start the Server: Launch the application by running the following command:
npm start

Now that the application is up and running, you can navigate to http://localhost:8000 to access a very simple interface built using jQuery, or visit http://localhost:8000/api-docs to access the Swagger interface for managing the REST API, or http://localhost:8000/graphql to open the graphical interface for GraphQL operations.

Features

Below is a list of the most important features of the application along with a description of how they work:

Simple Single Page interface

After opening the application in the browser http://localhost:8000, we are greeted with the following view:

image

At the top, there is a list of all books in the database, each with two buttons for deleting and editing. And below that, you'll find a form for creating a new book.

image
image

After entering values for the new book in the fields and clicking the OK button, it will be added to the list.

image

After clicking 'Edit,' new modal window for editing selected book appears.

image

image

After entering new data and clicking the OK button, the user is redirected back to the list view of books with the updated information in the edited book.

image

After clicking the X button next to a book, that book will be deleted.

GraphQL with Ruru

Below is the Ruru interface for GraphQL, check it out at http://localhost:8000/graphql.

image

It allows to test and execute queries in the dedicated GraphQL Query Language.

image

Above is an example for the 'getBooks' query. On the left side, you can select the fields that interest you and should be included in the response. In the middle, the constructed query is presented, which can also be manually edited. Upon clicking the arrow, you'll receive the response on the right side.

image

Here is an example for the 'getBook' query. On the left side, the required ID of the book was entered. In the middle, the query is constructed with the ID inserted into the correct place. On the right side, you can see the result for the created query.

image

Example of a mutator for creating a new book with the provided values for title and author.

image

Example of an update for a book with the specified ID and the new author, along with the result showing the new author and the old unchanged title.

image

Example for deleting a book with given ID.

Rest API with Swagger

When navigating to the path http://localhost:8000/api-docs, we will be greeted by the Swagger's graphical interface.

image
image

After expanding the GET operation for /api/books/, you'll see a 'Try it out' button. Upon clicking it, an 'Execute' button will appear, allowing you to send a request to the application.

image

After performing the operation, we receive a response containing a list of all books in the database as JSON.

image

For the 'POST' operation, which adds a new item, we need to specify a body parameter where we will pass the title and author for the new book.

image

In the response, we receive the newly created book along with its newly assigned ID.

image

For the 'GET' operation for a single book, we need to provide its ID as a parameter.

image

The response will include the book with the specified ID.

image

Next, for the 'PUT' operation, which is used for updating data, we need to provide the ID in the parameters and in the body, specify the values we want to change. In this case, we are only changing the author, while the title remains the same as before.

image

The result of the operation that includes the book with the changed author and original title.

image

The 'DELETE' operation, used for deleting books, here the ID parameter is required.

image

Here, the operation returns only a status code, in this case, 204 - No Content, indicating that there is no content present in the response body.

About

Application demonstrates GraphQL/Rest API integration for managing book data, with MongoDB, Swagger, and graphical interface.

Topics

Resources

Stars

Watchers

Forks