Using Spring WebFlux with GraphQL
What is GraphQL ?
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.It provides an alternative to REST APIs that is more focused on data, and provides a schema and a query language for clients to use.
GraphQL vs REST
REST API where you typically have a specific endpoint or resource you’re hitting that determines an entire block of data that comes back in the returning JSON response, which then needs to be parsed and scattered. GraphQL is instead established around schema, queries, and resolvers and rather aims to improve upon the REST philosophy by allowing you to ask for a specific piece of data — not just the entire block. No need for parsing through a long stream of data — you only get what you ask for. And what you ask for could be compiled from several different REST APIs.
What is Spring WebFlux?
Spring WebFlux is a web framework that’s built on top of Project Reactor, to give you asynchronous I/O, and allow your application to perform better.
What is Spring Data?
Spring Data is an umbrella project having number of sub-projects or modules all aimed towards providing uniform abstractions and uniform utility methods for the Data Access Layer in an application and support wide range of databases.
Demo Application
In order to get better acquainted with these technologies, we will develop Spring Boot WebFlux application with CRUD operations using GraphQL
Prerequisites
- Install JDK8 or higher
Spring Boot Application setup
- Initialize Spring Boot WebFlux project via Spring initializr
- Add
graphql
dependencies tobuild.gradle
. Since GraphQL starter is only experimental for now(will be included in 2.7 release)we need to add snapshot repositories to ourbuild.gradle
file.
H2 database interactions setup
- Add
h2
dependencies tobuild.gradle
- Create
schema.sql
file to create database table.
- Put
schema.sql
file intosrc/main/resources/
directory - Create
data.sql
file to seed data.
- Create
application.yml
or updateapplication.properties
file with appropriate configuration values.
- Create
Player
domain model to represent database item.
- Create interface
PlayerRepository
and extend in fromReactiveCrudRepository
.
GraphQL setup
Schema-First vs Object First
GraphQL provides a schema language that helps clients to create valid requests, enables the GraphiQL UI editor, promotes a common vocabulary across teams, and so on. It also brings up the age old schema vs object-first development dilemma.
Spring’s take is that schema-first development should be preferred. It facilitates a conversation among people of technical and non-technical background, it helps with tooling, it makes it easier to track changes, and so on.
- Put
schema.graphqls
file intosrc/main/resources/graphql
directory - Define schema mapping in Controller class for GraphQL queries and mutations using
@SchemaMapping
,@QueryMapping
,@MutationMapping
annotations.
GraphQL playground with GraphiQL
- Start service and open in browser
http://localhost:8080/graphiql
to open GraphiQL IDE
- Run
getAllPlayers()
query to get list of all players
- Run
addPlayer()
ordeletePlayerByName()
mutation to add/delete players
- Viola!
Source code can be found on GitHub: