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.
Below is step by step instruction on how to run this project on a local machine:
- 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.
- 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.
- 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.
- 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.
Below is a list of the most important features of the application along with a description of how they work:
After opening the application in the browser http://localhost:8000, we are greeted with the following view:
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.
After entering values for the new book in the fields and clicking the OK button, it will be added to the list.
After clicking 'Edit,' new modal window for editing selected book appears.
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.
After clicking the X button next to a book, that book will be deleted.
Below is the Ruru interface for GraphQL, check it out at http://localhost:8000/graphql.
It allows to test and execute queries in the dedicated GraphQL Query Language.
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.
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.
Example of a mutator for creating a new book with the provided values for title and author.
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.
Example for deleting a book with given ID.
When navigating to the path http://localhost:8000/api-docs, we will be greeted by the Swagger's graphical interface.
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.
After performing the operation, we receive a response containing a list of all books in the database as JSON.
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.
In the response, we receive the newly created book along with its newly assigned ID.
For the 'GET' operation for a single book, we need to provide its ID as a parameter.
The response will include the book with the specified ID.
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.
The result of the operation that includes the book with the changed author and original title.
The 'DELETE' operation, used for deleting books, here the ID parameter is required.
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.