Skip to content

Commit

Permalink
Request-scoped Instrumentations with GraphQLContext
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechhabarta committed Dec 14, 2017
1 parent 184025b commit 60f1093
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/main/java/graphql/servlet/DefaultInstrumentationProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package graphql.servlet;

import graphql.execution.instrumentation.Instrumentation;

public class DefaultInstrumentationProvider implements InstrumentationProvider {

private final Instrumentation instrumentation;

public DefaultInstrumentationProvider() {
this(null);
}

public DefaultInstrumentationProvider(Instrumentation instrumentation) {
this.instrumentation = instrumentation;
}

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

@Override
public Instrumentation getInstrumentation(GraphQLContext context) {
return getInstrumentation();
}
}
12 changes: 8 additions & 4 deletions src/main/java/graphql/servlet/GraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra
protected abstract Object createRootObject(Optional<HttpServletRequest> request, Optional<HttpServletResponse> response);
protected abstract ExecutionStrategyProvider getExecutionStrategyProvider();
protected abstract Instrumentation getInstrumentation();
protected Instrumentation getInstrumentation(GraphQLContext context) {
return getInstrumentation();
}

protected abstract GraphQLErrorHandler getGraphQLErrorHandler();
protected abstract PreparsedDocumentProvider getPreparsedDocumentProvider();
Expand Down Expand Up @@ -240,7 +243,8 @@ public String[] getMutations() {
@Override
public String executeQuery(String query) {
try {
final ExecutionResult result = newGraphQL(getSchemaProvider().getSchema()).execute(new ExecutionInput(query, null, createContext(Optional.empty(), Optional.empty()), createRootObject(Optional.empty(), Optional.empty()), new HashMap<>()));
final GraphQLContext context = createContext(Optional.empty(), Optional.empty());
final ExecutionResult result = newGraphQL(getSchemaProvider().getSchema(), context).execute(new ExecutionInput(query, null, context, createRootObject(Optional.empty(), Optional.empty()), new HashMap<>()));
return getMapper().writeValueAsString(createResultFromDataErrorsAndExtensions(result.getData(), result.getErrors(), result.getExtensions()));
} catch (Exception e) {
return e.getMessage();
Expand Down Expand Up @@ -282,13 +286,13 @@ private Optional<FileItem> getFileItem(Map<String, List<FileItem>> fileItems, St
return items.stream().findFirst();
}

private GraphQL newGraphQL(GraphQLSchema schema) {
private GraphQL newGraphQL(GraphQLSchema schema, GraphQLContext context) {
ExecutionStrategyProvider executionStrategyProvider = getExecutionStrategyProvider();
return GraphQL.newGraphQL(schema)
.queryExecutionStrategy(executionStrategyProvider.getQueryExecutionStrategy())
.mutationExecutionStrategy(executionStrategyProvider.getMutationExecutionStrategy())
.subscriptionExecutionStrategy(executionStrategyProvider.getSubscriptionExecutionStrategy())
.instrumentation(getInstrumentation())
.instrumentation(getInstrumentation(context))
.preparsedDocumentProvider(getPreparsedDocumentProvider())
.build();
}
Expand Down Expand Up @@ -336,7 +340,7 @@ private void query(String query, String operationName, Map<String, Object> varia
} else {
List<GraphQLServletListener.OperationCallback> operationCallbacks = runListeners(l -> l.onOperation(context, operationName, query, variables));

final ExecutionResult executionResult = newGraphQL(schema).execute(new ExecutionInput(query, operationName, context, rootObject, variables));
final ExecutionResult executionResult = newGraphQL(schema, context).execute(new ExecutionInput(query, operationName, context, rootObject, variables));
final List<GraphQLError> errors = executionResult.getErrors();
final Object data = executionResult.getData();
final Object extensions = executionResult.getExtensions();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/graphql/servlet/InstrumentationProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

public interface InstrumentationProvider {
Instrumentation getInstrumentation();
default Instrumentation getInstrumentation(GraphQLContext context) {
return getInstrumentation();
}
}
5 changes: 5 additions & 0 deletions src/main/java/graphql/servlet/OsgiGraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ protected Instrumentation getInstrumentation() {
return instrumentationProvider.getInstrumentation();
}

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

@Override
protected GraphQLErrorHandler getGraphQLErrorHandler() {
return errorHandler;
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/graphql/servlet/SimpleGraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.NoOpInstrumentation;
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
import graphql.execution.preparsed.PreparsedDocumentProvider;
import graphql.schema.GraphQLSchema;
Expand Down Expand Up @@ -60,9 +59,9 @@ public SimpleGraphQLServlet(GraphQLSchemaProvider schemaProvider, ExecutionStrat
this.executionStrategyProvider = executionStrategyProvider;

if (instrumentation == null) {
this.instrumentation = NoOpInstrumentation.INSTANCE;
this.instrumentationProvider = new NoOpInstrumentationProvider();
} else {
this.instrumentation = instrumentation;
this.instrumentationProvider = new DefaultInstrumentationProvider(instrumentation);
}

if(errorHandler == null) {
Expand Down Expand Up @@ -95,7 +94,7 @@ protected SimpleGraphQLServlet(Builder builder) {

this.schemaProvider = builder.schemaProvider;
this.executionStrategyProvider = builder.executionStrategyProvider;
this.instrumentation = builder.instrumentation;
this.instrumentationProvider = builder.instrumentationProvider;
this.errorHandler = builder.errorHandler;
this.contextBuilder = builder.contextBuilder;
this.rootObjectBuilder = builder.rootObjectBuilder;
Expand All @@ -104,7 +103,7 @@ protected SimpleGraphQLServlet(Builder builder) {

private final GraphQLSchemaProvider schemaProvider;
private final ExecutionStrategyProvider executionStrategyProvider;
private final Instrumentation instrumentation;
private final InstrumentationProvider instrumentationProvider;
private final GraphQLErrorHandler errorHandler;
private final GraphQLContextBuilder contextBuilder;
private final GraphQLRootObjectBuilder rootObjectBuilder;
Expand All @@ -131,7 +130,7 @@ public static class Builder {
private ExecutionStrategyProvider executionStrategyProvider = new DefaultExecutionStrategyProvider();
private ObjectMapperConfigurer objectMapperConfigurer;
private List<GraphQLServletListener> listeners;
private Instrumentation instrumentation = NoOpInstrumentation.INSTANCE;
private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider();
private GraphQLErrorHandler errorHandler = new DefaultGraphQLErrorHandler();
private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private GraphQLRootObjectBuilder rootObjectBuilder = new DefaultGraphQLRootObjectBuilder();
Expand All @@ -156,7 +155,12 @@ public Builder withObjectMapperConfigurer(ObjectMapperConfigurer configurer) {
}

public Builder withInstrumentation(Instrumentation instrumentation) {
this.instrumentation = instrumentation;
this.instrumentationProvider = new DefaultInstrumentationProvider(instrumentation);
return this;
}

public Builder withInstrumentationProvider(InstrumentationProvider instrumentationProvider) {
this.instrumentationProvider = instrumentationProvider;
return this;
}

Expand Down Expand Up @@ -212,7 +216,12 @@ protected ExecutionStrategyProvider getExecutionStrategyProvider() {

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

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

@Override
Expand Down

0 comments on commit 60f1093

Please sign in to comment.