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
9 changes: 6 additions & 3 deletions src/main/java/graphql/servlet/GraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.io.CharStreams;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphQLError;
import graphql.InvalidSyntaxError;
import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLSchema;
import graphql.validation.ValidationError;
Expand Down Expand Up @@ -57,6 +57,8 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static graphql.GraphQL.newGraphQL;

/**
* @author Andrew Potter
*/
Expand All @@ -72,6 +74,7 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra

protected abstract GraphQLContext createContext(Optional<HttpServletRequest> request, Optional<HttpServletResponse> response);
protected abstract ExecutionStrategy getExecutionStrategy();
protected abstract Instrumentation getInstrumentation();
protected abstract Map<String, Object> transformVariables(GraphQLSchema schema, String query, Map<String, Object> variables);

private final List<GraphQLOperationListener> operationListeners;
Expand Down Expand Up @@ -214,7 +217,7 @@ public String[] getMutations() {
@Override
public String executeQuery(String query) {
try {
final ExecutionResult result = new GraphQL(getSchema()).execute(query, createContext(Optional.empty(), Optional.empty()), new HashMap<>());
final ExecutionResult result = newGraphQL(getSchema()).instrumentation(getInstrumentation()).build().execute(query, createContext(Optional.empty(), Optional.empty()), new HashMap<>());
return mapper.writeValueAsString(createResultFromDataAndErrors(result.getData(), result.getErrors()));
} catch (Exception e) {
return e.getMessage();
Expand Down Expand Up @@ -268,7 +271,7 @@ private void query(String query, String operationName, Map<String, Object> varia
} else {
runListeners(operationListeners, l -> runListener(l, it -> it.beforeGraphQLOperation(context, operationName, query, variables)));

final ExecutionResult executionResult = new GraphQL(schema, getExecutionStrategy()).execute(query, operationName, context, transformVariables(schema, query, variables));
final ExecutionResult executionResult = newGraphQL(schema).queryExecutionStrategy(getExecutionStrategy()).instrumentation(getInstrumentation()).build().execute(query, operationName, context, transformVariables(schema, query, variables));
final List<GraphQLError> errors = executionResult.getErrors();
final Object data = executionResult.getData();

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/graphql/servlet/InstrumentationProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.servlet;

import graphql.execution.instrumentation.Instrumentation;

public interface InstrumentationProvider {
Instrumentation getInstrumentation();
}
26 changes: 26 additions & 0 deletions src/main/java/graphql/servlet/NoOpInstrumentationProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.servlet;

import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.NoOpInstrumentation;

public class NoOpInstrumentationProvider implements InstrumentationProvider {

@Override
public Instrumentation getInstrumentation() {
return NoOpInstrumentation.INSTANCE;
}
}
17 changes: 16 additions & 1 deletion src/main/java/graphql/servlet/OsgiGraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package graphql.servlet;

import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
Expand Down Expand Up @@ -48,7 +49,8 @@ public class OsgiGraphQLServlet extends GraphQLServlet {

private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider();

private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider();

private GraphQLSchema schema;
private GraphQLSchema readOnlySchema;

Expand Down Expand Up @@ -138,6 +140,14 @@ public void unsetExecutionStrategyProvider(ExecutionStrategyProvider provider) {
executionStrategyProvider = new EnhancedExecutionStrategyProvider();
}

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
public void setInstrumentationProvider(InstrumentationProvider provider) {
instrumentationProvider = provider;
}
public void unsetInstrumentationProvider(ExecutionStrategyProvider provider) {
instrumentationProvider = new NoOpInstrumentationProvider();
}

protected GraphQLContext createContext(Optional<HttpServletRequest> req, Optional<HttpServletResponse> resp) {
return contextBuilder.build(req, resp);
}
Expand All @@ -147,6 +157,11 @@ protected ExecutionStrategy getExecutionStrategy() {
return executionStrategyProvider.getExecutionStrategy();
}

@Override
protected Instrumentation getInstrumentation() {
return instrumentationProvider.getInstrumentation();
}

@Override
protected Map<String, Object> transformVariables(GraphQLSchema schema, String query, Map<String, Object> variables) {
return new GraphQLVariables(schema, query, variables);
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/graphql/servlet/SimpleGraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package graphql.servlet;

import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.NoOpInstrumentation;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

Expand All @@ -38,21 +40,28 @@ public class SimpleGraphQLServlet extends GraphQLServlet {
.build();

public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy) {
this(schema, executionStrategy, null, null);
this(schema, executionStrategy, null, null, null);
}

public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy, List<GraphQLOperationListener> operationListeners, List<GraphQLServletListener> servletListeners) {
public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy, List<GraphQLOperationListener> operationListeners, List<GraphQLServletListener> servletListeners, Instrumentation instrumentation) {
super(operationListeners, servletListeners, null);

this.schema = schema;
this.readOnlySchema = new GraphQLSchema(schema.getQueryType(), EMPTY_MUTATION_TYPE, schema.getDictionary());

this.executionStrategy = executionStrategy;

if (instrumentation == null) {
this.instrumentation = NoOpInstrumentation.INSTANCE;
} else {
this.instrumentation = instrumentation;
}
}

private final GraphQLSchema schema;
private final GraphQLSchema readOnlySchema;
private final ExecutionStrategy executionStrategy;
private final Instrumentation instrumentation;

@Override
public GraphQLSchema getSchema() {
Expand All @@ -74,6 +83,11 @@ protected ExecutionStrategy getExecutionStrategy() {
return executionStrategy;
}

@Override
protected Instrumentation getInstrumentation() {
return instrumentation;
}

@Override
protected Map<String, Object> transformVariables(GraphQLSchema schema, String query, Map<String, Object> variables) {
return variables;
Expand Down