A GraphQL Object
can have multiple Types
that can hold values or can be null
, GraphQL
provides a NonNull Type
, which makes sure the Input Type
will have values.
GraphQL Schema
declares non-null
variables by an exclamation mark !
and non-null
list is declared by exclamation mark !
with brackets [ ]
.
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.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.entity.AppleVO;
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();
}
@MutationMapping("addApple")
Apple addApple(@Argument AppleVO appleVO) {
return appleService.addApple(appleVO);
}
}
package org.wesome.graphql.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@Data
@EqualsAndHashCode
@AllArgsConstructor
public final class Apple {
private int appleId;
private String appleName;
private Boolean available;
private List<String> tastes;
private List<Vendor> vendors;
}
package org.wesome.graphql.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@Data
@EqualsAndHashCode
@AllArgsConstructor
public final class AppleVO {
private int appleId;
private String appleName;
private Boolean available;
private List<String> tastes;
private List<VendorVO> vendors;
}
package org.wesome.graphql.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode
@AllArgsConstructor
public final class Vendor {
private int vendorId;
private String vendorName;
private Boolean available;
}
package org.wesome.graphql.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode
@AllArgsConstructor
public final class VendorVO {
private String vendorName;
private Boolean available;
}
package org.wesome.graphql.service;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.entity.AppleVO;
import java.util.List;
public interface AppleService {
List<Apple> findAllApple();
Apple addApple(AppleVO appleVO);
}
package org.wesome.graphql.service;
import org.springframework.stereotype.Service;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.entity.AppleVO;
import org.wesome.graphql.entity.Vendor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class AppleServiceImpl implements AppleService {
private static final List<String> tastes = Arrays.asList("sweet", "sour", "bitter", "salty", "savory");
private static final List<Vendor> vendors = new ArrayList<>(List.of(new Vendor(1, "vendor A", true), new Vendor(2, "vendor B", true)));
private static final List<Apple> apples = new ArrayList<>(List.of(new Apple(1, "macintosh", true, tastes, vendors), new Apple(2, "fuji", false, tastes, vendors), new Apple(3, "gala", true, tastes, vendors), new Apple(4, "jonagold", false, tastes, vendors)));
@Override
public List<Apple> findAllApple() {
return apples;
}
@Override
public Apple addApple(AppleVO appleVO) {
List<Vendor> vendorList = new ArrayList<>();
appleVO.getVendors().forEach(vendor -> vendorList.add(new Vendor(vendorList.size() + 1, vendor.getVendorName(), vendor.getAvailable())));
Apple apple = new Apple(apples.size() + 1, appleVO.getAppleName(), appleVO.getAvailable(), appleVO.getTastes(), vendorList);
apples.add(apple);
return apple;
}
}
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
# Vendor Object
type Vendor{
# primary key of Vendor
vendorId:ID!
# vendor name
vendorName:String !
#vendor availability
available:Boolean!
}
# Apple Object
type Apple{
# primary key of apple
appleId:ID!
# available Boolean
available:Boolean!
# apple Name
appleName:String!
# apple Taste List
tastes:[String!] !
# vendors list
vendors:[Vendor!]!
}
# Apple Input Object
input AppleVO{
# available Boolean
available:Boolean!
# apple Name
appleName:String!
# apple Taste List
tastes:[String!]!
# vendors list
vendors:[VendorVO!]!
}
# Apple Input Object
input VendorVO{
# vendor name
vendorName:String!
#vendor availability
available:Boolean!
}
# Apple Query
type Query{
# query to get all apples
findAllApple:[Apple!]
}
# Apple Mutation
type Mutation{
# mutation to add an apple
addApple(appleVO:AppleVO):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>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 Postman,
in the query section add the below query
query FindAllApple {
findAllApple {
appleId
available
appleName
tastes
vendors {
vendorId
vendorName
available
}
}
}
For Mutation
, use the below query, and try to pass some variables or values as null.
mutation AddApple {
addApple(
appleVO: {
available: true
appleName: "macintosh A"
tastes: ["sweet", "sour", "bitter", "salty", "savory"]
vendors: [
{ vendorName: "vendor A", available: true }
{ vendorName: "vendor B", available: true }
]
}
) {
appleId
available
appleName
tastes
vendors {
vendorId
vendorName
available
}
}
}