Skip to content

Commit

Permalink
GraphQL endpoints logged on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Feb 17, 2021
1 parent 4e735f1 commit ba0c426
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WebInterceptor> interceptors) {
Expand All @@ -67,6 +72,10 @@ public RouterFunction<ServerResponse> 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)
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -75,6 +80,10 @@ public RouterFunction<ServerResponse> 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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ba0c426

Please sign in to comment.