The application consists of two Spring Boot Apps:
-
The
shopservice
. This service manages a list of shops (locations and beers they sell). It provides a REST API to query the shops. For simplicity there is no way to modify shops and the actual data is stored in memory only (no database). The purpose of the shop service is to demo, how to access a rest endpoint from GraphQL. -
The
beeradvisor
. This is the backend for our application. It manages Beers, Ratings and Users and provides a GraphQL API for our domain. It also add the Shops from theshopservice
to our GraphQL API by connecting to that service via REST.
The third project in this repository is the React-based frontend.
Ports
Before starting the services please make sure that the following ports are not in use:
- port 9000 for the
BeerAdvisor
backend - port 9010 for the
ShopService
- port 9080 for the frontend (webpack dev server)
Database
- When you start the
BeerAdvisor
backend an embedded h2 database will be started, that writes it's content to~/h2/beer-rating
. You can change that in theapplication.properties
file.
Users/Login
- In order to add Ratings (using GraphQL Mutations) you need to login via the UI. You can simply use any firstname of any author that already has submitted a rating (for example alessa, waldemar, lauren)
There are three bash scripts in this folder that helps you starting the application:
01_run_shopservice.sh
02_run_beeradvisor.sh
03_run_frontend.sh
When all three apps are running, you can access the web application at
http://localhost:9080
Alternatively you can run the application using gradle or your IDE:
Run shopservice
- Go to the
shopservice
directory ./gradlew bootRun
-
- OR - Just run
main
method inShopServiceApplication
class from your IDE (for example from SpringBoot launcher in IDEA)
- OR - Just run
Run beeradvisor
Application
- Go to the
beeradvisor
directory ./gradlew bootRun
-
- OR - Just run
main
method inBeerAdvisorApplication
class from your IDE (for example from SpringBoot launcher in IDEA)
- OR - Just run
Run the frontend
- Go to the
frontend
directory - Run
yarn install
(needed only for the first run) - Run
yarn start
After running the server, the GraphiQL API Explorer is available at http://localhost:9000/graphiql
-
schema definition with SDL:
rating.graphqls
-
Resolver for root fields (queries):
RatingQueryResolver
-
Resolver for mutations:
RatingMutationResolver
-
Using the Websocket Servlet for Subscriptions via Websocket:
ExampleGraphQLConfiguration.serverEndpointRegistration
(nearly the same as in the spring-boot-starter, only a little simplified for easier understanding) -
Resolver for subscriptions (each new rating is pushed to clients):
RatingSubscriptionResolver
-
Field resolver with arguments:
ShopBeerResolver.ratingsWithStars
(that should be in BeerFieldResolver but does not work due to graphl-java-tools bug: graphql-java-kickstart/graphql-java-tools#171) -
Extending types and root fields in by a second schema (modulariation of schemas): see
shop
subproject:shop.graphqls
extends the basic schema frombeerrating
ShopResolver
adds a field resolver for theBeer
type frombeerrating
(fieldshops
)ShopResolver.address
adds a field resolver for theShop
type, asaddress
(defined in the schema) does not exist on theShop
java class.
- Security for mutations using Spring Security:
RatingMutationResolver.addRating
The project contains of two apps:
-
shopservice
: A little "microservice", that knows a list ofShop
s and whatBeer
they sell. Theshopservice
provides a REST endpoint for requesting shops. -
beeradivsor
: the "real" application that providesBeer
,Rating
and the GraphQL API
The beeradivsor
project is setup as a multi module gradle project that consists of three Gradle modules:
-
beerrating
: this is our "core" domain. It consists of the Beer and Rating entities -
shop
: this is the GraphQL "gateway" to theshopservice
-
app
: this contains all necessary infrastructure code and configuration. It also configures the GraphQL servlet.
Both of the "domain" modules ship their own GraphQL API (so they are complete verticals), while "beerrating" is the base API that is extended by the "shop" module with the shop-specific types and queries. One could use the beerrating API without the shop api.
The third module is the Spring Boot Application module:
For demonstration purposes that project by default does not use the graphiql-spring-boot-starter
to configure
the GraphQL schema and servlet.
Instead the configuration is manually done in ExampleGraphQLConfiguration
inside the app
module.
To use the starter project instead:
- remove the
@Configuration
annotation fromExampleGraphQLConfiguration
- add the starter module to the gradle dependencies (
app/build.gradle
):compile compile 'com.graphql-java:graphql-spring-boot-starter:4.2.0'