{"id":18048052,"url":"https://github.com/ivangfr/springboot-graphql-databases","last_synced_at":"2025-04-10T09:47:11.267Z","repository":{"id":49875822,"uuid":"151987009","full_name":"ivangfr/springboot-graphql-databases","owner":"ivangfr","description":"The goal of this project is to explore GraphQL. For it, we will implement two microservices: author-book-api and book-review-api.","archived":false,"fork":false,"pushed_at":"2024-12-20T20:18:51.000Z","size":1277,"stargazers_count":27,"open_issues_count":0,"forks_count":25,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T08:42:25.551Z","etag":null,"topics":["docker","graphql","java","mongodb","mysql","spring-boot","spring-cloud-openfeign","spring-data-jpa","spring-data-mongodb","spring-graphql","spring-web-mvc","springdoc-openapi","zipkin"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivangfr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ivangfr"}},"created_at":"2018-10-07T21:29:20.000Z","updated_at":"2025-03-05T09:23:14.000Z","dependencies_parsed_at":"2023-10-03T16:56:33.328Z","dependency_job_id":"c01fc7ca-885f-413d-83ac-feeef8cfb509","html_url":"https://github.com/ivangfr/springboot-graphql-databases","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangfr%2Fspringboot-graphql-databases","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangfr%2Fspringboot-graphql-databases/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangfr%2Fspringboot-graphql-databases/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangfr%2Fspringboot-graphql-databases/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivangfr","download_url":"https://codeload.github.com/ivangfr/springboot-graphql-databases/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248196692,"owners_count":21063484,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","graphql","java","mongodb","mysql","spring-boot","spring-cloud-openfeign","spring-data-jpa","spring-data-mongodb","spring-graphql","spring-web-mvc","springdoc-openapi","zipkin"],"created_at":"2024-10-30T20:11:06.455Z","updated_at":"2025-04-10T09:47:11.241Z","avatar_url":"https://github.com/ivangfr.png","language":"Java","funding_links":["https://github.com/sponsors/ivangfr"],"categories":[],"sub_categories":[],"readme":"# springboot-graphql-databases\n\nThe goal of this project is to explore [`GraphQL`](https://graphql.org). For it, we will implement two [`Spring Boot`](https://docs.spring.io/spring-boot/index.html) Web Java applications: `author-book-api` and `book-review-api`.\n\n\u003e **Note**: In [`kubernetes-minikube-environment`](https://github.com/ivangfr/kubernetes-minikube-environment/tree/master/author-book-review-graphql) repository, it's shown how to deploy this project in `Kubernetes` (`Minikube`).\n\n## Proof-of-Concepts \u0026 Articles\n\nOn [ivangfr.github.io](https://ivangfr.github.io), I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.\n\n## Additional Readings\n\n- \\[**Medium**\\] [**Implementing and Securing a Spring Boot GraphQL API with Keycloak**](https://medium.com/@ivangfr/implementing-and-securing-a-spring-boot-graphql-api-with-keycloak-c461c86e3972)\n- \\[**Medium**\\] [**Implementing and Securing a Spring Boot GraphQL API with Okta**](https://medium.com/@ivangfr/implementing-and-securing-a-spring-boot-graphql-api-with-okta-78bc997359b4)\n\n## Project Diagram\n\n![project-diagram](documentation/project-diagram.jpeg)\n\n## Applications\n\n- ### author-book-api\n\n  `Spring Boot` Web Java application that handles `authors` and `books`. It exposes a `GraphQL` endpoint **and** traditional REST API endpoints. `author-book-api` uses [`MySQL`](https://www.mysql.com) as storage and calls `book-review-api` to get the reviews of the books. It uses [`Feign`](https://github.com/OpenFeign/feign) to easily create a client for `book-review-api` and [`Resilience4j`](https://github.com/resilience4j/resilience4j) (fault tolerance library) to handle fallback when `book-review-api` is down. The book `ISBN` is what connects books stored in `author-book-api` with the ones stored in `book-review-api`.\n\n- ### book-review-api\n\n  `Spring Boot` Web Java application that handles `books` and their `reviews`. It only exposes a `GraphQL` API and uses [`MongoDB`](https://www.mongodb.com) as storage.\n\n## Frontend applications\n\nIn the repository [`react-graphql-databases`](https://github.com/ivangfr/react-graphql-databases), I have implemented two [`ReactJS`](https://react.dev) applications `author-book-ui` and `book-review-ui` that are frontend applications for `author-book-api` and `book-review-api`, respectively.\n\nIf you want to see the complete communication frontend-backend using `GraphQL`, clone the `react-graphql-databases` and follow the README instructions.\n\n## Prerequisites\n\n- [`Java 21+`](https://www.oracle.com/java/technologies/downloads/#java21)\n- Some containerization tool [`Docker`](https://www.docker.com), [`Podman`](https://podman.io), etc.\n\n## Start Environment\n\n- Open a terminal and inside `springboot-graphql-databases` root folder run:\n  ```\n  docker compose up -d\n  ```\n\n- Wait for Docker containers to be up and running. To check it, run:\n  ```\n  docker ps -a\n  ```\n  \n## Run applications with Maven\n\nInside `springboot-graphql-databases`, run the following Maven commands in different terminals:\n\n- **author-book-api**\n  ```\n  ./mvnw clean spring-boot:run --projects author-book-api \\\n  -Dspring-boot.run.jvmArguments=\"-Dspring.datasource.username=authorbookuser -Dspring.datasource.password=authorbookpass\"\n  ```\n\n- **book-review-api**\n  ```\n  ./mvnw clean spring-boot:run --projects book-review-api \\\n  -Dspring-boot.run.jvmArguments=\"-Dspring.data.mongodb.username=bookreviewuser -Dspring.data.mongodb.password=bookreviewpass\"\n  ```\n  \n## Run Applications as Docker containers\n\n### Build Application's Docker Images\n\nIn a terminal and inside `springboot-graphql-databases` root folder, run the following script:\n```\n./build-docker-images.sh\n```\n      \n### Application's environment variables\n    \n- **author-book-api**\n\n  | Environment Variable   | Description                                                                          |\n  |------------------------|--------------------------------------------------------------------------------------|\n  | `MYSQL_HOST`           | Specify host of the `MySQL` database to use (default `localhost`)                    |\n  | `MYSQL_PORT`           | Specify port of the `MySQL` database to use (default `3306`)                         |\n  | `ZIPKIN_HOST`          | Specify host of the `Zipkin` distributed tracing system to use (default `localhost`) |\n  | `ZIPKIN_PORT`          | Specify port of the `Zipkin` distributed tracing system to use (default `9411`)      |\n  | `BOOK_REVIEW_API_HOST` | Specify host of the `book-review-api` service (default `localhost`)                  |\n  | `BOOK_REVIEW_API_PORT` | Specify port of the `book-review-api` service (default `9080`)                       |\n\n- **book-review-api**\n\n  | Environment Variable | Description                                                                          |\n  |----------------------|--------------------------------------------------------------------------------------|\n  | `MONGODB_HOST`       | Specify host of the `MongoDB` database to use (default `localhost`)                  |\n  | `MONGODB_PORT`       | Specify port of the `MongoDB` database to use (default `27017`)                      |\n  | `ZIPKIN_HOST`        | Specify host of the `Zipkin` distributed tracing system to use (default `localhost`) |\n  | `ZIPKIN_PORT`        | Specify port of the `Zipkin` distributed tracing system to use (default `9411`)      |\n\n### Start Applications as Docker containers\n\nIn a terminal and inside `springboot-graphql-databases` root folder, run following script:\n```\n./start-apps.sh\n```\n\n## Application's Link\n\n| Application     | URL Type | URL                                   |\n|-----------------|----------|---------------------------------------|\n| author-book-api | Swagger  | http://localhost:8080/swagger-ui.html |\n| author-book-api | GraphiQL | http://localhost:8080/graphiql        |\n| book-review-api | GraphiQL | http://localhost:9080/graphiql        |\n\n## How to use GraphiQL\n\n- **book-review-api**\n\n  1. In a browser, access http://localhost:9080/graphiql\n\n  2. Create a book and return its id:\n     ```\n     mutation {\n       createBook(bookInput: {title: \"Getting Started With Roo\", isbn: \"9781449307905\"}) {\n        id\n       }\n     }\n     ```\n\n  3. Add one review for the book created above, suppose the id is `5bd4bd4790e9f641b7388f23`:\n     ```\n     mutation {\n       addBookReview(bookId: \"5bd4bd4790e9f641b7388f23\", reviewInput: {reviewer: \"Ivan Franchin\", comment: \"It is a very good book\", rating: 5}) {\n         id\n       }\n     }\n     ```\n\n  4. Get all books stored in `book-review-api`, including their reviews:\n     ```\n     {\n       getBooks {\n         id\n         title\n         isbn\n         reviews {\n           comment\n           rating\n           reviewer\n           createdAt\n         }\n       }\n     }\n     ```\n\n- **author-book-api**\n\n  1. In a browser, access http://localhost:8080/graphiql\n\n  2. Create an author and return the author id:\n     ```\n     mutation {\n       createAuthor(authorInput: {name: \"Josh Long\"}) {\n         id\n       }\n     }\n     ```\n\n  3. Create a book and return the book id and author name:\n     \u003e **Note**: while creating this book in `author-book-api`, we are setting the same ISBN, `9781449307905`, as we did when creating the book in `book-review-api`.\n     ```\n     mutation {\n       createBook(bookInput: {authorId: 1, isbn: \"9781449307905\", title: \"Getting Started With Roo\", year: 2020}) {\n         id\n         author {\n           name\n         }\n       }\n     }\n     ```\n\n  4. Get author by id and return some information about his/her books including book reviews from `book-review-api`:\n     \u003e **Note**: as the book stored in `author-book-api` and `book-review-api` has the same ISBN, `9781449307905`, it's possible to retrieve the reviews of the book. Otherwise, an empty list will be returned in case `book-review-api` does not have a specific ISBN or the service is down. \n     ```\n     {\n       getAuthorById(authorId: 1) {\n         name\n         books {\n           isbn\n           title\n           bookReview {\n             reviews {\n               reviewer\n               rating\n               comment\n               createdAt\n             }\n           }\n         }\n       }\n     }\n     ```\n\n  5. Update book title and return its id and new title:\n     ```\n     mutation {\n       updateBook(bookId: 1, bookInput: {title: \"Getting Started With Roo 2\"}) {\n         id\n         title\n       }\n     }\n     ```\n\n  6. Delete the author and return author id:\n     ```\n     mutation {\n       deleteAuthor(authorId: 1) {\n         id\n       }\n     }\n     ```\n\n## Useful links \u0026 commands\n\n- **Zipkin**\n\n  It can be accessed at http://localhost:9411\n\n- **MySQL monitor**\n  ```\n  docker exec -it -e MYSQL_PWD=authorbookpass mysql mysql -uauthorbookuser --database authorbookdb\n  SHOW tables;\n  SELECT * FROM authors;\n  SELECT * FROM books;\n  ```\n  \u003e Type `exit` to get out of MySQL monitor\n\n- **MongoDB shell**\n  ```\n  docker exec -it mongodb mongosh -u bookreviewuser -p bookreviewpass --authenticationDatabase bookreviewdb\n  use bookreviewdb;\n  db.books.find().pretty();\n  ```\n  \u003e Type `exit` to get out of MongoDB shell\n\n## Shutdown\n\n- To stop applications:\n  - If they were started with `Maven`, go to the terminals where they are running and press `Ctrl+C`;\n  - If they were started as a Docker container, go to a terminal and, inside `springboot-graphql-databases` root folder, run the script below:\n    ```\n    ./stop-apps.sh\n    ```\n- To stop and remove docker compose containers, network and volumes, go to a terminal and, inside `springboot-graphql-databases` root folder, run the following command:\n  ```\n  docker compose down -v\n  ```\n\n## Cleanup\n\nTo remove the Docker images created by this project, go to a terminal and, inside `springboot-graphql-databases` root folder, run the following script:\n```\n./remove-docker-images.sh\n```\n\n## References\n\n- https://graphql.org/learn\n- https://www.pluralsight.com/resources/blog/guides/building-a-graphql-server-with-spring-boot\n- https://www.baeldung.com/spring-graphql\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivangfr%2Fspringboot-graphql-databases","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivangfr%2Fspringboot-graphql-databases","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivangfr%2Fspringboot-graphql-databases/lists"}