Pass input data to GraphQL query and Spring Boot Example

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

Complete example

Let’s create a step-by-step spring boot project and create a GraphQL Query that accepts the input.

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 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;


public class Home implements GraphQLQueryResolver{
	public String bio (String name, Long age) {
		return "My name is "+name+". I am "+age+" year(s) old";

Resolvers that contain the code that runs when an operation is executed. This resolver contains a method bio(), this method runs when the bio query is executed. This bio() takes two parameters name and age. We will pass values from the schema.

This class implements the interface GraphQLQueryResolver which makes this class a Resolver.

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 {
	bio(name: String, age: Long): String

We will pass name and age values to the bio 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-

That’s it for this post. I hope you learned about GraphQL and Spring Boot. If you like this post, then please share this post with your friends and colleagues, also share this with your social media profile.

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