|
1 | 1 | package com.oembedler.moon.graphql.boot; |
2 | 2 |
|
| 3 | +import graphql.GraphQL; |
| 4 | +import graphql.kickstart.execution.GraphQLInvoker; |
3 | 5 | import graphql.kickstart.execution.GraphQLObjectMapper; |
4 | | -import graphql.kickstart.execution.GraphQLQueryInvoker; |
5 | | -import graphql.kickstart.execution.subscription.SubscriptionConnectionListener; |
| 6 | +import graphql.kickstart.execution.config.GraphQLBuilder; |
| 7 | +import graphql.kickstart.execution.subscriptions.GraphQLSubscriptionInvocationInputFactory; |
| 8 | +import graphql.kickstart.execution.subscriptions.SubscriptionConnectionListener; |
| 9 | +import graphql.kickstart.execution.subscriptions.apollo.KeepAliveSubscriptionConnectionListener; |
6 | 10 | import graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration; |
| 11 | +import graphql.schema.GraphQLSchema; |
7 | 12 | import graphql.servlet.GraphQLWebsocketServlet; |
8 | | -import graphql.servlet.apollo.ApolloSubscriptionConnectionListener; |
9 | | -import graphql.servlet.input.GraphQLInvocationInputFactory; |
| 13 | +import java.time.Duration; |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.Set; |
| 18 | +import javax.websocket.server.ServerContainer; |
10 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
11 | 20 | import org.springframework.beans.factory.annotation.Value; |
12 | 21 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
13 | | -import org.springframework.boot.autoconfigure.condition.*; |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 23 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 24 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
14 | 26 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
15 | 27 | import org.springframework.context.annotation.Bean; |
16 | 28 | import org.springframework.context.annotation.Conditional; |
|
19 | 31 | import org.springframework.web.socket.server.standard.ServerEndpointExporter; |
20 | 32 | import org.springframework.web.socket.server.standard.ServerEndpointRegistration; |
21 | 33 |
|
22 | | -import javax.websocket.server.ServerContainer; |
23 | | -import java.time.Duration; |
24 | | - |
25 | 34 | @Configuration |
26 | 35 | @ConditionalOnWebApplication |
27 | 36 | @ConditionalOnClass(DispatcherServlet.class) |
|
31 | 40 | @EnableConfigurationProperties(GraphQLSubscriptionApolloProperties.class) |
32 | 41 | public class GraphQLWebsocketAutoConfiguration { |
33 | 42 |
|
34 | | - @Value("${graphql.servlet.subscriptions.websocket.path:/subscriptions}") |
35 | | - private String websocketPath; |
| 43 | + @Value("${graphql.servlet.subscriptions.websocket.path:/subscriptions}") |
| 44 | + private String websocketPath; |
36 | 45 |
|
37 | | - @Autowired |
38 | | - private GraphQLSubscriptionApolloProperties apolloProperties; |
| 46 | + @Autowired |
| 47 | + private GraphQLSubscriptionApolloProperties apolloProperties; |
39 | 48 |
|
40 | | - @Bean |
41 | | - @ConditionalOnMissingBean |
42 | | - public SubscriptionConnectionListener subscriptionConnectionListener() { |
43 | | - if (!apolloProperties.isKeepAliveEnabled()) { |
44 | | - return ApolloSubscriptionConnectionListener.createWithKeepAliveDisabled(); |
45 | | - } |
46 | | - return ApolloSubscriptionConnectionListener.createWithKeepAliveInterval(Duration.ofSeconds(apolloProperties.getKeepAliveIntervalSeconds())); |
47 | | - } |
| 49 | + @Bean |
| 50 | + @ConditionalOnMissingBean |
| 51 | + public GraphQLInvoker graphQLInvoker(GraphQLSchema schema) { |
| 52 | + GraphQL graphQL = new GraphQLBuilder().build(schema); |
| 53 | + return new GraphQLInvoker(graphQL); |
| 54 | + } |
48 | 55 |
|
49 | | - @Bean |
50 | | - @ConditionalOnMissingBean |
51 | | - public GraphQLWebsocketServlet graphQLWebsocketServlet(GraphQLInvocationInputFactory invocationInputFactory, GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQLObjectMapper, SubscriptionConnectionListener subscriptionConnectionListener) { |
52 | | - return new GraphQLWebsocketServlet(queryInvoker, invocationInputFactory, graphQLObjectMapper, subscriptionConnectionListener); |
| 56 | + @Bean |
| 57 | + @ConditionalOnMissingBean |
| 58 | + public GraphQLWebsocketServlet graphQLWebsocketServlet( |
| 59 | + GraphQLSubscriptionInvocationInputFactory invocationInputFactory, |
| 60 | + GraphQLInvoker graphQLInvoker, |
| 61 | + GraphQLObjectMapper graphQLObjectMapper, |
| 62 | + @Autowired(required = false) Collection<SubscriptionConnectionListener> connectionListeners) { |
| 63 | + Set<SubscriptionConnectionListener> listeners = new HashSet<>(); |
| 64 | + if (connectionListeners != null) { |
| 65 | + listeners.addAll(connectionListeners); |
53 | 66 | } |
| 67 | + keepAliveListener().ifPresent(listeners::add); |
| 68 | + return new GraphQLWebsocketServlet(graphQLInvoker, invocationInputFactory, graphQLObjectMapper, listeners); |
| 69 | + } |
54 | 70 |
|
55 | | - @Bean |
56 | | - @ConditionalOnClass(ServerContainer.class) |
57 | | - public ServerEndpointRegistration serverEndpointRegistration(GraphQLWebsocketServlet servlet) { |
58 | | - return new GraphQLWsServerEndpointRegistration(websocketPath, servlet); |
| 71 | + private Optional<SubscriptionConnectionListener> keepAliveListener() { |
| 72 | + if (apolloProperties.isKeepAliveEnabled()) { |
| 73 | + return Optional.of(new KeepAliveSubscriptionConnectionListener( |
| 74 | + Duration.ofSeconds(apolloProperties.getKeepAliveIntervalSeconds())) |
| 75 | + ); |
59 | 76 | } |
| 77 | + return Optional.empty(); |
| 78 | + } |
60 | 79 |
|
61 | | - @Bean |
62 | | - @ConditionalOnMissingBean |
63 | | - @ConditionalOnClass(ServerContainer.class) |
64 | | - public ServerEndpointExporter serverEndpointExporter() { |
65 | | - return new ServerEndpointExporter(); |
66 | | - } |
| 80 | + @Bean |
| 81 | + @ConditionalOnClass(ServerContainer.class) |
| 82 | + public ServerEndpointRegistration serverEndpointRegistration(GraphQLWebsocketServlet servlet) { |
| 83 | + return new GraphQLWsServerEndpointRegistration(websocketPath, servlet); |
| 84 | + } |
| 85 | + |
| 86 | + @Bean |
| 87 | + @ConditionalOnMissingBean |
| 88 | + @ConditionalOnClass(ServerContainer.class) |
| 89 | + public ServerEndpointExporter serverEndpointExporter() { |
| 90 | + return new ServerEndpointExporter(); |
| 91 | + } |
67 | 92 |
|
68 | 93 | } |
0 commit comments