Skip to content

Commit

Permalink
Make GraphQL execution strategies configurable by an 'ExecutionStrate…
Browse files Browse the repository at this point in the history
…gyProvider' singleton (#47)

* .Make GraphQL execution strategies configurable by an `ExecutionStrategyProvider` singleton

* Removed repeated checks for executionStrategyProvider being null.
  • Loading branch information
logi authored and NicholasAzar committed Aug 24, 2018
1 parent 8b7c2db commit a86e2cb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
@@ -0,0 +1,28 @@
package com.networknt.graphql.router;

import graphql.execution.ExecutionStrategy;

/**
* ExecutionStrategyProvider interface that is used to inject execution strategy
* implementations into the framework. The service module will pass these on to
* the graphql-java framework.
*
* @author Logi Ragnarsson
*/
public interface ExecutionStrategyProvider {

/**
* Return an execution strategy to use for queries or null to use the default.
*/
ExecutionStrategy getQueryExecutionStrategy();

/**
* Return an execution strategy to use for mutations or null to use the default.
*/
ExecutionStrategy getMutationExecutionStrategy();

/**
* Return an execution strategy to use for subscriptions or null to use the default.
*/
ExecutionStrategy getSubscriptionExecutionStrategy();
}
Expand Up @@ -2,6 +2,7 @@

import com.networknt.config.Config;
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.SchemaProvider;
Expand All @@ -10,6 +11,7 @@
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.execution.ExecutionStrategy;
import graphql.schema.GraphQLSchema;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -37,6 +39,9 @@ public class GraphqlPostHandler implements HttpHandler {

private static final Logger logger = LoggerFactory.getLogger(GraphqlPostHandler.class);
static GraphQLSchema schema = null;
static ExecutionStrategy queryExecutionStrategy = null;
static ExecutionStrategy mutationExecutionStrategy = null;
static ExecutionStrategy subscriptionExecutionStrategy = null;

static {
// load GraphQL Schema with service loader. It should be defined in service.yml
Expand All @@ -48,6 +53,14 @@ 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");
}

// Replace default execution strategies if so configured.
ExecutionStrategyProvider executionStrategyProvider = SingletonServiceFactory.getBean(ExecutionStrategyProvider.class);
if(executionStrategyProvider != null) {
queryExecutionStrategy = executionStrategyProvider.getQueryExecutionStrategy();
mutationExecutionStrategy = executionStrategyProvider.getMutationExecutionStrategy();
subscriptionExecutionStrategy = executionStrategyProvider.getSubscriptionExecutionStrategy();
}
}

@Override
Expand Down Expand Up @@ -92,6 +105,15 @@ private GraphQL getGraphql() {
if (InstrumentationLoader.graphqlInstrumentation != null) {
graphql = graphql.instrumentation(InstrumentationLoader.graphqlInstrumentation);
}
if(queryExecutionStrategy != null) {
graphql.queryExecutionStrategy(queryExecutionStrategy);
}
if(mutationExecutionStrategy != null) {
graphql.mutationExecutionStrategy(mutationExecutionStrategy);
}
if(subscriptionExecutionStrategy != null) {
graphql.subscriptionExecutionStrategy(subscriptionExecutionStrategy);
}
return graphql.build();
}
}

0 comments on commit a86e2cb

Please sign in to comment.