From ba0c426c3207fa084b95dc66b34701c8d4aca8b0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 16 Feb 2021 21:39:13 +0000 Subject: [PATCH] GraphQL endpoints logged on startup Closes gh-24 --- .../boot/WebFluxGraphQLAutoConfiguration.java | 15 ++++++++++++++- .../boot/WebMvcGraphQLAutoConfiguration.java | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQLAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQLAutoConfiguration.java index 9e85dc0..f582488 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQLAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQLAutoConfiguration.java @@ -19,6 +19,8 @@ import java.util.stream.Collectors; import graphql.GraphQL; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import reactor.core.publisher.Mono; import org.springframework.beans.factory.ObjectProvider; @@ -54,6 +56,9 @@ @AutoConfigureAfter(GraphQLAutoConfiguration.class) public class WebFluxGraphQLAutoConfiguration { + private static final Log logger = LogFactory.getLog(WebFluxGraphQLAutoConfiguration.class); + + @Bean @ConditionalOnMissingBean public GraphQLHttpHandler graphQLHandler(GraphQL.Builder graphQLBuilder, ObjectProvider interceptors) { @@ -67,6 +72,10 @@ public RouterFunction graphQLEndpoint( String path = properties.getPath(); Resource resource = resourceLoader.getResource("classpath:graphiql/index.html"); + if (logger.isInfoEnabled()) { + logger.info("GraphQL endpoint HTTP POST " + path); + } + return RouterFunctions.route() .GET(path, req -> ServerResponse.ok().bodyValue(resource)) .POST(path, accept(MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_JSON)), handler::handleQuery) @@ -92,8 +101,12 @@ public GraphQLWebSocketHandler graphQLWebSocketHandler( public HandlerMapping graphQLWebSocketEndpoint( GraphQLWebSocketHandler handler, GraphQLProperties properties) { + String path = properties.getWebsocket().getPath(); + if (logger.isInfoEnabled()) { + logger.info("GraphQL endpoint WebSocket " + path); + } WebSocketHandlerMapping handlerMapping = new WebSocketHandlerMapping(); - handlerMapping.setUrlMap(Collections.singletonMap(properties.getWebsocket().getPath(), handler)); + handlerMapping.setUrlMap(Collections.singletonMap(path, handler)); handlerMapping.setOrder(-2); // Ahead of HTTP endpoint ("routerFunctionMapping" bean) return handlerMapping; } diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQLAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQLAutoConfiguration.java index cb6585c..26eea71 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQLAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQLAutoConfiguration.java @@ -23,6 +23,8 @@ import javax.websocket.server.ServerContainer; import graphql.GraphQL; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -61,6 +63,9 @@ @AutoConfigureAfter(GraphQLAutoConfiguration.class) public class WebMvcGraphQLAutoConfiguration { + private static final Log logger = LogFactory.getLog(WebMvcGraphQLAutoConfiguration.class); + + @Bean @ConditionalOnMissingBean public GraphQLHttpHandler graphQLHandler(GraphQL.Builder graphQLBuilder, @@ -75,6 +80,10 @@ public RouterFunction graphQLQueryEndpoint( String path = properties.getPath(); Resource resource = resourceLoader.getResource("classpath:graphiql/index.html"); + if (logger.isInfoEnabled()) { + logger.info("GraphQL endpoint HTTP POST " + path); + } + return RouterFunctions.route() .GET(path, req -> ServerResponse.ok().body(resource)) .POST(path, contentType(MediaType.APPLICATION_JSON).and(accept(MediaType.APPLICATION_JSON)), handler::handle) @@ -105,9 +114,12 @@ public GraphQLWebSocketHandler graphQLWebSocketHandler( @Bean public HandlerMapping graphQLWebSocketEndpoint(GraphQLWebSocketHandler handler, GraphQLProperties properties) { + String path = properties.getWebsocket().getPath(); + if (logger.isInfoEnabled()) { + logger.info("GraphQL endpoint WebSocket " + path); + } WebSocketHandlerMapping handlerMapping = new WebSocketHandlerMapping(); - handlerMapping.setUrlMap(Collections.singletonMap( - properties.getWebsocket().getPath(), + handlerMapping.setUrlMap(Collections.singletonMap(path, new WebSocketHttpRequestHandler(handler, new DefaultHandshakeHandler()))); handlerMapping.setOrder(2); // Ahead of HTTP endpoint ("routerFunctionMapping" bean) return handlerMapping;