GraphQL Schema Custom Scaler Json

GraphQL Object Scaler allows to have Objects without defining them in GraphQL Schema. some developers prefer their Objects in JSON format, hence GraphQL Scaler also provides Json Scaler, its same as GraphQL Object Scaler. Json Scaler is useful when unknown data is coming from the backend system but can't provide the actual GraphQL Schema definition for it. But Since it's not described in GraphQL Schema, therefore, Json Scaler cannot be queried.

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.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.json.simple.JSONObject;

@Data
@EqualsAndHashCode
@AllArgsConstructor
public final class Apple {
    private int appleId;
    private String appleName;
    private Boolean available;
    private Float price;
    private JSONObject vendor;
    private Object 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.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import org.wesome.graphql.entity.Apple;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AppleServiceImpl implements AppleService {
    private static JSONObject vendor = new JSONObject(Map.of("vendorId", 1, "vendorName", "vendor A"));
    private static JSONObject address = new JSONObject(Map.of("streetAddress", "21 2nd Street", "city", "New York", "state", "NY", "postalCode", "10021"));
    private static final List<Apple> apples = new ArrayList<>(List.of(new Apple(1, "Macintosh", true, 1.1F, vendor, address), new Apple(2, "Fuji", true, 2.2F, vendor, address), new Apple(3, "Gala", true, 2.2F, vendor, address), new Apple(4, "Jonagold", true, 3.3F, vendor, address)));

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

import graphql.scalars.ExtendedScalars;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.graphql.execution.RuntimeWiringConfigurer;

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

    @Bean
    public RuntimeWiringConfigurer runtimeWiringConfigurer() {
        return wiringBuilder -> wiringBuilder.scalar(ExtendedScalars.Json);
    }
}

\src\main\resources\graphql\schema.graphqls

#"""
#A custom scalar must be declared explicitly.
#"""
scalar JSON
# Apple Object
type Apple{
    # primary key of apple
    appleId:ID!
    # apple Name
    appleName:String
    # Custom Scalar JSON
    vendor: JSON
    address: JSON
}

# Apple Query
type Query{
    # query to get all apples
    findAllApple:[Apple]
}

\src\main\resources\application.properties

spring.graphql.graphiql.enabled=true
<?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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-extended-scalars</artifactId>
            <version>21.0</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </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>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

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
        vendor
        address
    }
}

follow us on