Spring Boot GraphQL Max Query Depth Instrumentation

GraphQL supports nested objects as a response, but sometimes a restriction is required to limit the depth of the response. Spring Boot GraphQL provides inbuild MaxQueryDepthInstrumentation that adds a limit to the nested query and throws an error if the query is greater than the specified values.

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 graphql.analysis.MaxQueryDepthInstrumentation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyGraphQLConfiguration {
    @Bean
    MaxQueryDepthInstrumentation maxQueryDepthInstrumentation() {
        return new MaxQueryDepthInstrumentation(3);
    }
}
package org.wesome.graphql.entity;

public record Address(String buildingNumber, String city, String cityName, String country) {
}
package org.wesome.graphql.entity;

public record Apple(int appleId, String appleName, Float price, Boolean available, Vendor vendor) {
}
package org.wesome.graphql.entity;

public record Vendor(int vendorId, String vendorName, Address address) {
}
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.List;

import static org.wesome.graphql.GraphqlProjectApplication.apples;

@Service
public class AppleServiceImpl implements AppleService {
    @Override
    public List<Apple> findAllApple() {
        return apples;
    }
}
package org.wesome.graphql;

import com.github.javafaker.Faker;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.wesome.graphql.entity.Address;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.entity.Vendor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@SpringBootApplication
public class GraphqlProjectApplication implements CommandLineRunner {
    public static List<Apple> apples;

    public static void main(String[] args) {
        SpringApplication.run(GraphqlProjectApplication.class, args);
    }

    @Override
    public void run(String... args) {
        Faker faker = new Faker();
        List<Apple> list = new ArrayList<>();
        IntStream.rangeClosed(1, 10).forEach(value -> {
            Address address = new Address(faker.address().buildingNumber(), faker.address().city(), faker.address().cityName(), faker.address().country());
            Vendor vendor = new Vendor(value, faker.name().fullName(), address);
            Apple apple = new Apple(value, faker.food().fruit(), Float.valueOf(value), faker.random().nextBoolean(), vendor);
            list.add(apple);
        });
        apples = list;
    }
}
# Address Object
type Address{
    # building number
    buildingNumber:String
    # vendor country name
    cityName:String
    # vendor country name
    country:String
}

# Vendor Object
type Vendor{
    # vendor id
    vendorId:ID!
    # vendor name
    vendorName:String
    # vendor Address
    address:Address
}

# Apple Object
type Apple{
    # primary key of apple
    appleId:ID!
    # apple Name
    appleName:String
    # apple price
    price:Float
    # apple Availability
    available:Boolean
    #vendor Details
    vendor: Vendor
}

# 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>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        vendor {\r\n            vendorId\r\n            vendorName\r\n        }\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 Postmanin the query section add the below query

query FindAllApple {
    findAllApple {
        appleId
        appleName
        price
        available
        vendor {
            vendorId
            vendorName
        }
    }
}

follow us on