GraphQL endpoints logged on startup

Closes gh-24
This commit is contained in:
Rossen Stoyanchev
2021-02-16 21:39:13 +00:00
parent 4e735f1ca8
commit ba0c426c32
2 changed files with 28 additions and 3 deletions

View File

@@ -19,6 +19,8 @@ import java.util.Collections;
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 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
@AutoConfigureAfter(GraphQLAutoConfiguration.class)
public class WebFluxGraphQLAutoConfiguration {
private static final Log logger = LogFactory.getLog(WebFluxGraphQLAutoConfiguration.class);
@Bean
@ConditionalOnMissingBean
public GraphQLHttpHandler graphQLHandler(GraphQL.Builder graphQLBuilder, ObjectProvider<WebInterceptor> interceptors) {
@@ -67,6 +72,10 @@ public class WebFluxGraphQLAutoConfiguration {
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 class WebFluxGraphQLAutoConfiguration {
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;
}

View File

@@ -23,6 +23,8 @@ import javax.servlet.http.HttpServletRequest;
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 @@ import static org.springframework.web.servlet.function.RequestPredicates.content
@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 class WebMvcGraphQLAutoConfiguration {
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 class WebMvcGraphQLAutoConfiguration {
@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;