Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The goal of this project is to explore GraphQL. For it, we will implement two microservices: author-book-api and book-review-api.

NotificationsYou must be signed in to change notification settings

ivangfr/springboot-graphql-databases

Repository files navigation

The goal of this project is to exploreGraphQL. For it, we will implement twoSpring Boot Web Java applications:author-book-api andbook-review-api.

Note: Inkubernetes-minikube-environment repository, it's shown how to deploy this project inKubernetes (Minikube).

Proof-of-Concepts & Articles

Onivangfr.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.

Additional Readings

Project Diagram

project-diagram

Applications

  • author-book-api

    Spring Boot Web Java application that handlesauthors andbooks. It exposes aGraphQL endpointand traditional REST API endpoints.author-book-api usesMySQL as storage and callsbook-review-api to get the reviews of the books. It usesFeign to easily create a client forbook-review-api andResilience4j (fault tolerance library) to handle fallback whenbook-review-api is down. The bookISBN is what connects books stored inauthor-book-api with the ones stored inbook-review-api.

  • book-review-api

    Spring Boot Web Java application that handlesbooks and theirreviews. It only exposes aGraphQL API and usesMongoDB as storage.

Frontend applications

In the repositoryreact-graphql-databases, I have implemented twoReactJS applicationsauthor-book-ui andbook-review-ui that are frontend applications forauthor-book-api andbook-review-api, respectively.

If you want to see the complete communication frontend-backend usingGraphQL, clone thereact-graphql-databases and follow the README instructions.

Prerequisites

Start Environment

  • Open a terminal and insidespringboot-graphql-databases root folder run:

    docker compose up -d
  • Wait for Docker containers to be up and running. To check it, run:

    docker ps -a

Run applications with Maven

Insidespringboot-graphql-databases, run the following Maven commands in different terminals:

  • author-book-api

    ./mvnw clean spring-boot:run --projects author-book-api \-Dspring-boot.run.jvmArguments="-Dspring.datasource.username=authorbookuser -Dspring.datasource.password=authorbookpass"
  • book-review-api

    ./mvnw clean spring-boot:run --projects book-review-api \-Dspring-boot.run.jvmArguments="-Dspring.data.mongodb.username=bookreviewuser -Dspring.data.mongodb.password=bookreviewpass"

Run Applications as Docker containers

Build Application's Docker Images

In a terminal and insidespringboot-graphql-databases root folder, run the following script:

./build-docker-images.sh

Application's environment variables

  • author-book-api

    Environment VariableDescription
    MYSQL_HOSTSpecify host of theMySQL database to use (defaultlocalhost)
    MYSQL_PORTSpecify port of theMySQL database to use (default3306)
    ZIPKIN_HOSTSpecify host of theZipkin distributed tracing system to use (defaultlocalhost)
    ZIPKIN_PORTSpecify port of theZipkin distributed tracing system to use (default9411)
    BOOK_REVIEW_API_HOSTSpecify host of thebook-review-api service (defaultlocalhost)
    BOOK_REVIEW_API_PORTSpecify port of thebook-review-api service (default9080)
  • book-review-api

    Environment VariableDescription
    MONGODB_HOSTSpecify host of theMongoDB database to use (defaultlocalhost)
    MONGODB_PORTSpecify port of theMongoDB database to use (default27017)
    ZIPKIN_HOSTSpecify host of theZipkin distributed tracing system to use (defaultlocalhost)
    ZIPKIN_PORTSpecify port of theZipkin distributed tracing system to use (default9411)

Start Applications as Docker containers

In a terminal and insidespringboot-graphql-databases root folder, run following script:

./start-apps.sh

Application's Link

ApplicationURL TypeURL
author-book-apiSwaggerhttp://localhost:8080/swagger-ui.html
author-book-apiGraphiQLhttp://localhost:8080/graphiql
book-review-apiGraphiQLhttp://localhost:9080/graphiql

How to use GraphiQL

  • book-review-api

    1. In a browser, accesshttp://localhost:9080/graphiql

    2. Create a book and return its id:

      mutation {  createBook(bookInput: {title: "Getting Started With Roo", isbn: "9781449307905"}) {   id  }}
    3. Add one review for the book created above, suppose the id is5bd4bd4790e9f641b7388f23:

      mutation {  addBookReview(bookId: "5bd4bd4790e9f641b7388f23", reviewInput: {reviewer: "Ivan Franchin", comment: "It is a very good book", rating: 5}) {    id  }}
    4. Get all books stored inbook-review-api, including their reviews:

      {  getBooks {    id    title    isbn    reviews {      comment      rating      reviewer      createdAt    }  }}
  • author-book-api

    1. In a browser, accesshttp://localhost:8080/graphiql

    2. Create an author and return the author id:

      mutation {  createAuthor(authorInput: {name: "Josh Long"}) {    id  }}
    3. Create a book and return the book id and author name:

      Note: while creating this book inauthor-book-api, we are setting the same ISBN,9781449307905, as we did when creating the book inbook-review-api.

      mutation {  createBook(bookInput: {authorId: 1, isbn: "9781449307905", title: "Getting Started With Roo", year: 2020}) {    id    author {      name    }  }}
    4. Get author by id and return some information about his/her books including book reviews frombook-review-api:

      Note: as the book stored inauthor-book-api andbook-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 casebook-review-api does not have a specific ISBN or the service is down.

      {  getAuthorById(authorId: 1) {    name    books {      isbn      title      bookReview {        reviews {          reviewer          rating          comment          createdAt        }      }    }  }}
    5. Update book title and return its id and new title:

      mutation {  updateBook(bookId: 1, bookInput: {title: "Getting Started With Roo 2"}) {    id    title  }}
    6. Delete the author and return author id:

      mutation {  deleteAuthor(authorId: 1) {    id  }}

Useful links & commands

  • Zipkin

    It can be accessed athttp://localhost:9411

  • MySQL monitor

    docker exec -it -e MYSQL_PWD=authorbookpass mysql mysql -uauthorbookuser --database authorbookdbSHOW tables;SELECT * FROM authors;SELECT * FROM books;

    Typeexit to get out of MySQL monitor

  • MongoDB shell

    docker exec -it mongodb mongosh -u bookreviewuser -p bookreviewpass --authenticationDatabase bookreviewdbuse bookreviewdb;db.books.find().pretty();

    Typeexit to get out of MongoDB shell

Shutdown

  • To stop applications:
    • If they were started withMaven, go to the terminals where they are running and pressCtrl+C;
    • If they were started as a Docker container, go to a terminal and, insidespringboot-graphql-databases root folder, run the script below:
      ./stop-apps.sh
  • To stop and remove docker compose containers, network and volumes, go to a terminal and, insidespringboot-graphql-databases root folder, run the following command:
    docker compose down -v

Cleanup

To remove the Docker images created by this project, go to a terminal and, insidespringboot-graphql-databases root folder, run the following script:

./remove-docker-images.sh

References

About

The goal of this project is to explore GraphQL. For it, we will implement two microservices: author-book-api and book-review-api.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp