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.
Table of Contents
Complete example
Let’s create a step-by-step spring boot project and Add validation to the GraphQL schema
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 Request class
Create SampleRequest.java
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 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;
import in.bushansirgur.springboot.model.SampleRequest;
@Service
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-
—
—
Hi,
I want to validate graphql schema and “graphql-java-tools” dependency is not working to validate it.
For example input object added in type object.
Could you please suggest some plugins who can help to detect schema related issue while building any application.