Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.networknt.graphql.router;

import graphql.ExecutionResult;
import io.undertow.server.HttpServerExchange;

public interface GraphqlCustomHandler {
void handleResponse(HttpServerExchange exchange, ExecutionResult result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.networknt.graphql.common.GraphqlUtil;
import com.networknt.graphql.router.ExecutionStrategyProvider;
import com.networknt.graphql.common.InstrumentationLoader;
import com.networknt.graphql.common.InstrumentationProvider;
import com.networknt.graphql.router.GraphqlCustomHandler;
import com.networknt.graphql.router.SchemaProvider;
import com.networknt.service.SingletonServiceFactory;
import com.networknt.status.Status;
Expand Down Expand Up @@ -42,6 +42,7 @@ public class GraphqlPostHandler implements HttpHandler {
static ExecutionStrategy queryExecutionStrategy = null;
static ExecutionStrategy mutationExecutionStrategy = null;
static ExecutionStrategy subscriptionExecutionStrategy = null;
static GraphqlCustomHandler customHandler = null;

static {
// load GraphQL Schema with service loader. It should be defined in service.yml
Expand All @@ -53,6 +54,10 @@ public class GraphqlPostHandler implements HttpHandler {
logger.error("Unable to load GraphQL schema - no SchemaProvider implementation in service.yml");
throw new RuntimeException("Unable to load GraphQL schema - no SchemaProvider implementation in service.yml");
}
GraphqlCustomHandler handler = SingletonServiceFactory.getBean(GraphqlCustomHandler.class);
if(handler != null ){
customHandler = handler;
}

// Replace default execution strategies if so configured.
ExecutionStrategyProvider executionStrategyProvider = SingletonServiceFactory.getBean(ExecutionStrategyProvider.class);
Expand Down Expand Up @@ -88,16 +93,20 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
String operationName = (String)requestParameters.get(GRAPHQL_REQUEST_OP_NAME_KEY);
ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(query).operationName(operationName).context(exchange).root(exchange).variables(variables).build();
ExecutionResult executionResult = graphQL.execute(executionInput);
Map<String, Object> result = new HashMap<>();
if (executionResult.getErrors().size() > 0) {
result.put(GRAPHQL_RESPONSE_ERROR_KEY, executionResult.getErrors());
logger.error("Errors: {}", executionResult.getErrors());
if (customHandler != null) {
customHandler.handleResponse(exchange, executionResult);
} else {
result.put(GRAPHQL_RESPONSE_DATA_KEY, executionResult.getData());
Map<String, Object> result = new HashMap<>();
if (executionResult.getErrors().size() > 0) {
result.put(GRAPHQL_RESPONSE_ERROR_KEY, executionResult.getErrors());
logger.error("Errors: {}", executionResult.getErrors());
} else {
result.put(GRAPHQL_RESPONSE_DATA_KEY, executionResult.getData());
}
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.setStatusCode(StatusCodes.OK);
exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(result));
}
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.setStatusCode(StatusCodes.OK);
exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(result));
}

private GraphQL getGraphql() {
Expand Down