Spring Boot GraphQL
allows custom instrumentation
by implementing an Instrumentation Interface
. All Custom Instrumentation must have a @Component annotation, Spring boot will register all Components at startup time. Below is an example of Spring Boot GraphQL Custom Instrumentation
for query response
and computation time
taken.
package org.wesome.graphql.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.service.AppleService;
import java.util.List;
@Controller
public class AppleGraphQLController {
@Autowired
private AppleService appleService;
@QueryMapping("findAllApple")
List<Apple> findAllApple() {
return appleService.findAllApple();
}
}
package org.wesome.graphql.configuration;
import com.google.gson.Gson;
import graphql.ExecutionResult;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimplePerformantInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.MessageFormat;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
@Component
public class QueryInstrumentation extends SimplePerformantInstrumentation {
@Autowired
private Gson gson;
@Override
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters, InstrumentationState state) {
return new InstrumentationContext<>() {
AtomicLong startTime = new AtomicLong();
@Override
public void onDispatched(CompletableFuture<ExecutionResult> result) {
startTime.set(System.currentTimeMillis());
}
@Override
public void onCompleted(ExecutionResult result, Throwable t) {
System.out.println(MessageFormat.format("Query execution time {1} millisecond, Response is {0} ", gson.toJson(result), System.currentTimeMillis() - startTime.get()));
}
};
}
}
package org.wesome.graphql.entity;
public record Apple(int appleId, String appleName, Float price, Boolean available) {
}
package org.wesome.graphql.service;
import org.wesome.graphql.entity.Apple;
import java.util.List;
public interface AppleService {
List<Apple> findAllApple();
}
package org.wesome.graphql.service;
import org.springframework.stereotype.Service;
import org.wesome.graphql.entity.Apple;
import java.util.ArrayList;
import java.util.List;
@Service
public class AppleServiceImpl implements AppleService {
private static final List<Apple> apples = new ArrayList<>(List.of(new Apple(1, "Macintosh", 1.1F, true), new Apple(2, "Fuji", 2.2F, true), new Apple(3, "Gala", 3.3F, true), new Apple(4, "Jonagold", 4.4F, true)));
@Override
public List<Apple> findAllApple() {
return apples;
}
}
package org.wesome.graphql;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.wesome.graphql.entity.Apple;
import java.util.List;
@SpringBootApplication
public class GraphqlProjectApplication {
public static void main(String[] args) {
SpringApplication.run(GraphqlProjectApplication.class, args);
}
@Bean
public Gson gson() {
return new GsonBuilder().serializeNulls().setPrettyPrinting().create();
}
}
# Apple Object
type Apple{
# primary key of apple
appleId:ID!
# apple Name
appleName:String
# apple price
price:Float
# apple Availability
available:Boolean
}
# Apple Query
type Query{
# query to get all apples
findAllApple:[Apple]
}
\src\main\resources\application.properties
spring.graphql.graphiql.enabled=true
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>3.1.5</version>
<relativePath/>
</parent>
<groupId>org.wesome</groupId>
<artifactId>spring-boot-graphql</artifactId>
<version>0.0.1-snapshot</version>
<name>spring-boot-graphql</name>
<description>implementing graphql in spring boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Rest API
curl --location --request POST 'http://localhost:8080/graphql' \
--header 'Content-Type: application/json' \
--data '{"query":"query FindAllApple {\r\n findAllApple {\r\n appleId\r\n appleName\r\n price\r\n available\r\n }\r\n}\r\n","variables":{}}'
GraphiQL
GraphQL
provides an inbuild UserInterface GraphiQL
, which can be accessed via http://localhost:8080/graphiql
or access GraphQL via Postman,
in the query section add the below query
query FindAllApple {
findAllApple {
appleId
appleName
price
available
}
}