Spring Boot GraphQL Subscription Operation
provides a bi-directional
communication over Transmission Control Protocal or TCP
socket. The Subscription Operation
connects the client
to Server
via HTTP WebSocket
and keeps on pushing data on time interval
whereas a Spring Boot GraphQL Query Operation
is a one-time pull.
Learn more about Spring Boot GraphQL Query Operation
.
Facebook builds GraphQL Subscription Operation to provide real-time “Likes” to users without refreshing the page.
package org.wesome.graphql.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
import org.springframework.stereotype.Controller;
import org.wesome.graphql.entity.Apple;
import org.wesome.graphql.service.AppleService;
import reactor.core.publisher.Flux;
@Controller
public class AppleGraphQLController {
@Autowired
private AppleService appleService;
@SubscriptionMapping("appleSubscribe")
public Flux<Apple> appleSubscribe() {
return appleService.findLatest();
}
}
package org.wesome.graphql.entity;
public record Apple(int appleId, String appleName, String taste, String now) {
}
package org.wesome.graphql.service;
import org.wesome.graphql.entity.Apple;
import reactor.core.publisher.Flux;
public interface AppleService {
Flux<Apple> findLatest();
}
package org.wesome.graphql.service;
import org.springframework.stereotype.Service;
import org.wesome.graphql.entity.Apple;
import reactor.core.publisher.Flux;
import java.time.Duration;
import java.time.LocalTime;
import java.util.stream.Stream;
@Service
public class AppleServiceImpl implements AppleService {
@Override
public Flux<Apple> findLatest() {
return Flux.fromStream(Stream.generate(() -> new Apple(1, "Fuji", "sweet", LocalTime.now().toString()))).delayElements(Duration.ofSeconds(2));
}
}
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
# current time
now : String
}
# Apple Subscription
type Subscription {
appleSubscribe:Apple
}
\src\main\resources\application.properties
spring.graphql.graphiql.enabled=true
spring.graphql.websocket.path=/graphiql
<?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.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>6.0.2</version>
</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
subscription appleSubscribe {
appleSubscribe {
appleId
appleName
taste
now
}
}