GraphQL Variables Default Value

GraphQL provides an option to add default values to the variables, so just in case the variable is not defined, the default value will be used.

package org.wesome.graphql.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
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;
import java.util.Optional;

@Controller
public class AppleGraphQLController {
    @Autowired
    private AppleService appleService;

    @QueryMapping("findAppleById")
    Optional<Apple> findAppleById(@Argument int appleId) {
        return appleService.findAppleById(appleId);
    }

    @QueryMapping("findAppleByName")
    List<Apple> findAppleById(@Argument String appleName) {
        return appleService.findAppleByName(appleName);
    }

    @QueryMapping("findAppleByIdAndName")
    List<Apple> findAppleByIdAndName(@Argument int appleId, @Argument String appleName) {
        return appleService.findAppleByIdAndName(appleId, appleName);
    }

    @QueryMapping("findAllApple")
    List<Apple> findAllApple() {
        return appleService.findAllApple();
    }
}
package org.wesome.graphql.entity;

public record Apple(int appleId, String appleName, String taste) {
}
package org.wesome.graphql.service;

import org.wesome.graphql.entity.Apple;

import java.util.List;
import java.util.Optional;

public interface AppleService {

    Optional<Apple> findAppleById(int appleId);

    List<Apple> findAppleByName(String appleName);

    List<Apple> findAppleByIdAndName(int appleId, String appleName);

    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;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class AppleServiceImpl implements AppleService {
    private static final List<Apple> apples = new ArrayList<>(List.of(new Apple(1, "Macintosh", "sweet"), new Apple(2, "Fuji", "tangy"), new Apple(3, "Gala", "bitter"), new Apple(4, "Jonagold", "sour")));

    @Override
    public Optional<Apple> findAppleById(int id) {
        return apples.stream().filter(apple -> apple.appleId() == id).findFirst();

    }

    @Override
    public List<Apple> findAppleByName(String appleName) {
        return apples.stream().filter(apple -> apple.appleName().equalsIgnoreCase(appleName)).collect(Collectors.toList());
    }

    @Override
    public List<Apple> findAppleByIdAndName(int appleId, String appleName) {
        return apples.stream().filter(apple -> apple.appleId() == appleId).filter(apple -> apple.appleName().equalsIgnoreCase(appleName)).collect(Collectors.toList());
    }

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

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

# Apple Object
type Apple{
    # primary key of apple
    appleId:ID!
    # apple Name
    appleName:String
    # apple Taste
    taste:String
}
# Apple Query
type Query{
    # query to get all apples
    findAllApple:[Apple]
    # query to get apple by id
    findAppleById(appleId:ID!):Apple
    # query to get apple by name
    findAppleByName(appleName:String):[Apple]
    # query to get apple by Id and Name
    findAppleByIdAndName(appleId: ID!,appleName:String):[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>
    <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>
        <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>
    <name>spring boot graphql</name>
    <description>implementing graphql in spring boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <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

For variable, declare the below JSON

{
    "appleId": 1,
    "appleName": "Macintosh"
}

now Query will be able to use the variables declared, let's see

Query Operation with Variable Query Operation without Variable Query Operation without Variable Default Value
query FindAppleById($appleId: Int) {
    findAppleById(appleId: $appleId) {
        appleId
        appleName
        taste
    }
}
query FindAppleById {
    findAppleById(appleId: 1) {
        appleId
        appleName
        taste
    }
}
query FindAppleById($appleId: ID! = 1) {
    findAppleById(appleId: $appleId) {
        appleId
        appleName
        taste
    }
}

Query to Find Apple By Name

Query Operation with Variable Query Operation without Variable Query Operation without Variable Default Value
query FindAppleByName($appleName: String) {
    findAppleByName(appleName: $appleName) {
        appleId
        appleName
        taste
    }
}
query FindAppleByName {
    findAppleByName(appleName: "Macintosh") {
        appleId
        appleName
        taste
    }
}
query FindAppleByName($appleName: String = "Macintosh") {
    findAppleByName(appleName: $appleName) {
        appleId
        appleName
        taste
    }
}

Query to Find Apple By Name and appleId 

Query Operation with Variable Query Operation without Variable Query Operation without Variable Default Value
query FindAppleById($appleId: Int) {
    findAppleById(appleId: $appleId) {
        appleId
        appleName
        taste
    }
}
query FindAppleById {
    findAppleById(appleId: 1) {
        appleId
        appleName
        taste
    }
}
query FindAppleByIdAndName(
    $appleId: ID! = 1
    $appleName: String = "Macintosh"
) {
    findAppleByIdAndName(appleId: $appleId, appleName: $appleName) {
        appleId
        appleName
        taste
    }
}

follow us on