All Web Services
support the Addition
of new data, Updation
, and Deletion
of existing data. Spring Boot GraphQL
Mutation Operation
to support Insert
, Update
, and Delete
.
Spring Boot GraphQL Mutation Operation
is equivalent to POST
, PUT
, PATCH
, and DELETE
methods in REST API
or INSERT
, UPDATE
, and DELETE
commands in the Database
. Mutation Operation
is POST
in nature.
GraphQL works on the Data Access Layer and does not bother about business functionality.
Spring Boot GraphQL Mutation Operation
is used with arguments
, variables
, fragments
, interfaces
, and inputs
. Spring Boot GraphQL Schema Input Object
can only be used with mutations
. Learn more about Spring Boot GraphQL Schema Input Object
a sample GraphQL Mutation Operation
looks like as below.
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);
}
}
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("findApple")
Apple findApple(@Argument int appleId) {
return appleService.findApple(appleId);
}
@QueryMapping("findAllApple")
List<Apple> findAllApple() {
return appleService.findAllApple();
}
@MutationMapping("addApple")
Apple addApple(@Argument AppleVO appleVO) {
return appleService.addApple(appleVO);
}
@MutationMapping("updateApple")
Apple updateApple(@Argument Apple apple) {
return appleService.updateApple(apple);
}
}
package org.wesome.graphql.entity;
import java.util.Objects;
public record Apple(int appleId, String appleName, String taste) {
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (Apple) obj;
return this.appleId == that.appleId &&
Objects.equals(this.appleName, that.appleName) &&
Objects.equals(this.taste, that.taste);
}
@Override
public String toString() {
return "Apple[" +
"appleId=" + appleId + ", " +
"appleName=" + appleName + ", " +
"taste=" + taste + ']';
}
}
package org.wesome.graphql.entity;
import java.util.Objects;
public record AppleVO(int appleId, String appleName, String taste) {
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (AppleVO) obj;
return this.appleId == that.appleId && Objects.equals(this.appleName, that.appleName) && Objects.equals(this.taste, that.taste);
}
@Override
public String toString() {
return "AppleVO[" + "appleId=" + appleId + ", " + "appleName=" + appleName + ", " + "taste=" + taste + ']';
}
}
package org.wesome.graphql.service;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.entity.AppleVO;
import java.util.List;
public interface AppleService {
Apple findApple(int id);
List<Apple> findAllApple();
Apple addApple(AppleVO appleVO);
Apple updateApple(Apple apple);
}
package org.wesome.graphql.service;
import org.springframework.stereotype.Service;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.entity.AppleVO;
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", "sweet"), new Apple(2, "Fuji", "tangy"), new Apple(3, "Gala", "bitter"), new Apple(4, "Jonagold", "sour")));
@Override
public Apple findApple(int id) {
return apples.get(id);
}
@Override
public List<Apple> findAllApple() {
return apples;
}
@Override
public Apple addApple(AppleVO appleVO) {
Apple apple = new Apple(apples.size(), appleVO.appleName(), appleVO.taste());
apples.add(apple);
return apple;
}
@Override
public Apple updateApple(AppleVO appleVO) {
Apple app = new Apple(appleVO.appleId(), appleVO.appleName(), appleVO.taste());
apples.set(appleVO.appleId() - 1, app);
return app;
}
}
\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 Input Object
input AppleVO{
# primary key of apple
appleId:ID
# apple Name
appleName:String
# apple Taste
taste:String
}
# Apple Mutation
type Mutation{
# mutation to add an apple
addApple(appleVO:AppleVO):Apple
# query to update an apple
updateApple(appleVO:AppleVO):Apple
}
# Apple Query
type Query{
# query to get all apples
findAllApple:[Apple]
# query to get apple by id
findApple(appleId:Int):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.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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
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
taste
}
}
For Mutation Operation
of Addtion
, use the below query
mutation AddApple {
addApple(appleVO: { appleName: "PinkLady", taste: "sweet" }) {
appleId
appleName
taste
}
}
For Mutation Operation
of Updation
, use the below query
mutation UpdateApple {
updateApple(appleVO: { appleId: "3", appleName: "PinkLady", taste: "sweet" }) {
appleId
appleName
taste
}
}