Pass JSON to GraphQL Query with Spring Boot Example

Hey guys in this post, we will discuss passing JSON data to the GraphQL query with Spring Boot Example

Complete example

Let’s create a step-by-step spring boot project and pass JSON data to GraphQL query

Read More:

Create spring boot project

There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies

Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring data jpa</description>



spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.

graphql-spring-boot-starter dependency is the starter dependency for GraphQL and spring boot. graphiql-spring-boot-starter dependency provides the interface to test GraphQL queries. graphql-java-tools dependency helps us to scan the graphql files having the extension .graphqls

Create a Request class

Create inside the in.bushansirgur.springboot.model package and add the following content

package in.bushansirgur.springboot.model;

public class SampleRequest {

	private String firstName;
	private String lastName;

	public String getFirstName() {
		return firstName;

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public String getLastName() {
		return lastName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

This class is a simple POJO class, contains private fields, setters, getters and toString().

Create a service class

Create inside the in.bushansirgur.springboot.resolver package and add the following content

package in.bushansirgur.springboot.resolver;

import org.springframework.stereotype.Service;


import in.bushansirgur.springboot.model.SampleRequest;

public class Home implements GraphQLQueryResolver{
	public String fullName(SampleRequest sRequest) {
		return sRequest.getFirstName() + " " + sRequest.getLastName();


Resolver method fullName() takes the argument of type SampleRequest.

Create a schema

Create home.graphqls file inside the schema folder and place it inside the resources folder

Note: filename and folder doesn’t matter here you can create with any file name and folder but it should have the extension .graphqls

type Query {
	fullName(sampleRequest: SampleRequest): String
input SampleRequest {
	firstName: String,
	lastName: String

We also need to provide the fields for the type SampleRequest, this will be of input type because the type SampleRequest is an input to the fullName() query.

Run the app

Run the application using the below maven command –

mvn spring-boot:run

Open the Altair plugin in chrome and execute the following query-

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply