Using Spring WebFlux with GraphQL

Pavel Klindziuk
Dandelion Tutorials
3 min readDec 2, 2021

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 to build.gradle. Since GraphQL starter is only experimental for now(will be included in 2.7 release)we need to add snapshot repositories to our build.gradle file.
Spring GraphQL starter related dependencies

H2 database interactions setup

  • Add h2dependencies to build.gradle
H2 database related dependencies
  • Create schema.sql file to create database table.
Script to create table, automatically picked and executed by Spring Data
  • Put schema.sql file into src/main/resources/directory
  • Create data.sql file to seed data.
Script to fill table with mock data, automatically picked and executed by Spring Data
  • Create application.yml or update application.properties file with appropriate configuration values.
Configuration in application.yml file
  • Create Player domain model to represent database item.
Player class
  • Create interface PlayerRepository and extend in from ReactiveCrudRepository.
PlayerRepository class

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.

GraphQL schema
  • Put schema.graphqls file into src/main/resources/graphql directory
  • Define schema mapping in Controller class for GraphQL queries and mutations using@SchemaMapping , @QueryMapping , @MutationMapping annotations.
PlayerGraphQL Controller class

GraphQL playground with GraphiQL

GraphiQL main page
  • Run getAllPlayers() query to get list of all players
Execution of getAllPlayers() graphql query
  • Run addPlayer() or deletePlayerByName() mutation to add/delete players
Execution of addPlayer() graphql mutation
Execution of deletePlayerByName() mutation
  • Viola!

Source code can be found on GitHub:

--

--