Hey guys in this post, we will discuss passing input data to the GraphQL query with Spring Boot Example.
Table of Contents
Complete example
Let’s create a step-by-step spring boot project and create a GraphQL Query that accepts the input.
Read More:
- Check the complete GraphQL and Spring Boot Tutorials
- Check the complete Spring Boot Data JPA Tutorials
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.bushansirgur</groupId>
<artifactId>springbootgraphql</artifactId>
<version>v1</version>
<name>springbootgraphql</name>
<description>Spring data jpa</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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 Home.java inside the in.bushansirgur.springboot.resolver package and add the following content
package in.bushansirgur.springboot.resolver;
import org.springframework.stereotype.Service;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
@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.
