Add Validations to the GraphQL Schema with Example

Hey guys in this post, we will add validations to the GraphQL schema with Spring Boot Example. This is a continuation of the previous post, please follow that post before proceeding with this.

Complete example

Let’s create a step-by-step spring boot project and Add validation to the GraphQL schema

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

	public String home () {
		return null;

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
        home: String!
input SampleRequest {
	firstName: String!,
	lastName: String

We will add ![Exclamtory] at the end of the type indicating that it should not return null. It should always return non-nullable value.

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-



About the author

Bushan Sirgur

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

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *