HOW GRAPHQL WORKS IN JAVA(SPRING BOOT)?

Rukmal Senavirathne
API Integration Essentials
4 min readNov 3, 2020

--

GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. (query language is not a tool or a framework for designing APIs). In simple words, the client can get what he wants in GraphQL.

GraphQL contains three main parts.

1. Schema definition language or SDL

SDL is used to define a GraphQL schema.

GraphQL schema contains,

Types which are similar to the classes in Java

Operations which can be a performance on those types similar to methods in Java,

· Query — Query for reading operations

· Mutation — Mutation for write operations

· Object — Object for define types

2. Runtime environment

Parsing the graphQL schema file and creating an ‘In memory schema’ from it.

Executing the operations in the client request.

3. Query language

The query language is used by the client to use operations that are defined in the GraphQL schema.

GraphQL in Java

Schema File

Schema file contains Types and operations that can be performed on Types.

Schema Parser

Schema parser is provided by the GraphQL Java API. Create an object of schema parser class to pass the schema file.

Type Definition Registry

After parsing the schema file can get an instance of Type Definition Registry. This instance is an Inmemory representation of the schema file.

Data Fetcher

Data Fetcher is a callback function. It is for operation defined in the schema file. Each data fetcher is either associated with an operation or with the field which are defined in the schema file.

Type Wiring

Type Wiring represents linking between the operation and the data fetcher or between fields and data fetcher. Each instance of type wiring represents a linking.

RunTime Wiring

Feed all the instances of type wiring to the Run-Time Wiring.

Schema Generator

Combine Type Definition Registry and RunTime Wiring with help of Schema generator and Create an instance of GraphQL Schema.

GraphQL

GraphQL is an instance of GraphQL Schema. This is the main class in the GraphQL Java API which is used to handle client requests.

The flow of the GraphQL Application

The client sends a request either query or mutation. The request does not connect directly with the GraphQL. It interacts with the controller that contains the method to pass the request body to the GraphQL instance.

GraphQL executes the DataFetcher if the operation mention in the request is valid. Data Fetcher fetches the data from DB or API and returns the data.

The controller creates a response from the result and sends it to the client.

Example

  1. First, create a schema file there are two types Book and Author, There are two read operations getBook and getBooks , There are four write operations createBook, deleteAuthor , UpdateBook, and UpdateAuthor.

2. Next, Create a GraphQL Controller that contains the method for pass the request body to the GraphQL instance.

3. Next, Create a schema parser instance. Then with the parse method get TypedefinitionRegistry. This is the in-memory representation of the GraphQL schema file.

4. There are seven types of wirings for linking data fetchers and operations. Then with type wirings create an instance of Runtime Wiring instance.

5. Then create a schema generator to combine the runtime Wiring and Type Definition Registry. Then create an instance of GraphQL schema. From graphQL schema create and GraphQL.

6. Next, Create Data Fetcher there are two Data Fetches. These data fetchers map to getBook and getBooks operations which are defined in the schema file.

7. Repositories for a deal with the Database or APIs.

For more information for the above example follow the GitHub link:

https://github.com/rukmals/GraphQLDemoSpringBoot

--

--

Rukmal Senavirathne
API Integration Essentials

Graduated from the Department of Computer Science and Engineering at the University of Moratuwa.