Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 2daf1c0

Browse files
committed
Properties for setting subscription keep alive
1 parent 0f63510 commit 2daf1c0

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

example-graphql-subscription/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ spring:
22
application:
33
name: graphql-subscription-example
44
server:
5-
port: 9000
5+
port: 9001
66

77
graphql:
88
servlet:

example-graphql-subscription/src/main/resources/public/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
}
6363

6464
function subscribeToStocks() {
65+
// var exampleSocket = new WebSocket("ws://ceofs-seb-dot-shell-dtest.appspot.com:443/subscriptions");
66+
// var exampleSocket = new WebSocket("ws://173.194.79.153:80/subscriptions");
6567
var exampleSocket = new WebSocket("ws://localhost:9000/subscriptions");
6668
networkBlip();
6769

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.oembedler.moon.graphql.boot;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties("graphql.servlet.subscriptions.apollo")
8+
class GraphQLSubscriptionApolloProperties {
9+
10+
private boolean keepAliveEnabled = true;
11+
12+
private int keepAliveIntervalSeconds = 15;
13+
14+
}

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLToolsProperties.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.springframework.context.annotation.Configuration;
66

77
@Data
8-
@Configuration
98
@ConfigurationProperties(prefix = "graphql.tools")
109
class GraphQLToolsProperties {
1110

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLWebsocketAutoConfiguration.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.oembedler.moon.graphql.boot;
22

33
import graphql.schema.GraphQLSchema;
4+
import graphql.servlet.ApolloSubscriptionConnectionListener;
45
import graphql.servlet.GraphQLInvocationInputFactory;
56
import graphql.servlet.GraphQLObjectMapper;
67
import graphql.servlet.GraphQLQueryInvoker;
78
import graphql.servlet.GraphQLWebsocketServlet;
9+
import graphql.servlet.SubscriptionConnectionListener;
10+
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.beans.factory.annotation.Value;
912
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
1013
import org.springframework.boot.autoconfigure.condition.*;
14+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1115
import org.springframework.context.annotation.Bean;
1216
import org.springframework.context.annotation.Configuration;
1317
import org.springframework.web.servlet.DispatcherServlet;
@@ -17,18 +21,32 @@
1721
import javax.websocket.ContainerProvider;
1822
import javax.websocket.WebSocketContainer;
1923
import javax.websocket.server.ServerContainer;
24+
import java.time.Duration;
2025

2126
@Configuration
2227
@ConditionalOnWebApplication
2328
@ConditionalOnClass(DispatcherServlet.class)
2429
@ConditionalOnBean({GraphQLSchema.class})
2530
@ConditionalOnProperty(value = "graphql.servlet.websocket.enabled", havingValue = "true", matchIfMissing = true)
2631
@AutoConfigureAfter({GraphQLJavaToolsAutoConfiguration.class})
32+
@EnableConfigurationProperties(GraphQLSubscriptionApolloProperties.class)
2733
public class GraphQLWebsocketAutoConfiguration {
2834

2935
@Value("${graphql.servlet.subscriptions.websocket.path:/subscriptions}")
3036
private String websocketPath;
3137

38+
@Autowired
39+
private GraphQLSubscriptionApolloProperties apolloProperties;
40+
41+
@Bean
42+
@ConditionalOnMissingBean
43+
public SubscriptionConnectionListener subscriptionConnectionListener() {
44+
if (!apolloProperties.isKeepAliveEnabled()) {
45+
return ApolloSubscriptionConnectionListener.createWithKeepAliveDisabled();
46+
}
47+
return ApolloSubscriptionConnectionListener.createWithKeepAliveInterval(Duration.ofSeconds(apolloProperties.getKeepAliveIntervalSeconds()));
48+
}
49+
3250
@Bean
3351
@ConditionalOnMissingBean
3452
public GraphQLWebsocketServlet graphQLWebsocketServlet(GraphQLInvocationInputFactory invocationInputFactory, GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQLObjectMapper) {

0 commit comments

Comments
 (0)