Create a GraphQL Query with Spring Boot and Test it using Altair Plugin




Hey guys in this post, we will create the very first GraphQL Query with Spring Boot and Test it using the Altair plugin

Overview


GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015

Read more about GraphQL in their official website

Features


Following are the features that are provided by GraphQL

>> A query language for your API


GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

>> Ask for what you need, get exactly that


Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.

>> Get many resources in a single request


GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.

>> Describe what’s possible with a type system


GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. GraphQL uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors. Apps can use types to avoid writing manual parsing code.

Complete example


Let’s create a step-by-step spring boot project and create the very first 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="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 home () {
		return "this is home page contents";
	}
}

Resolvers that contain the code that runs when an operation is executed. This resolver contains a method home(), this method runs when the home query is executed

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 {
	home: String
}

There are 3 operations that can be performed in GraphQL.

  • Query
  • Mutation
  • Subscription

Query allows us to fetch the data. Mutation allows us to change the data. Subscription allows us to watch the data for changes

This is where we will define all the operations we want to serve. Its also where we will define any custom types of our application needs. For now, the only type defined is the built-in Query type

All our queries need to be defined in the Query type. The query definition is made up of two parts, a query name and query type.

The query name can be anything we like, but it should be descriptive. The second part is the query definition, which is the type of the data that we get.

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Open the chrome browser and open apps and search for Altair –

Screenshot-2021-07-02-at-1-16-46-PM

Install this plugin, this will allow us to test GraphQL queries

In this plugin, by default the URL will be http://localhost:8080/graphql

Screenshot-2021-07-02-at-1-22-38-PM

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