From 87b64a5a6d92937534f4bc142865ee87bec11577 Mon Sep 17 00:00:00 2001 From: Rogers Date: Sat, 6 May 2017 00:57:08 -0400 Subject: [PATCH 1/3] Added support for instrumentation --- .../java/graphql/servlet/GraphQLServlet.java | 6 ++++- .../servlet/InstrumentationProvider.java | 21 +++++++++++++++ .../servlet/NoopInstrumentationProvider.java | 26 +++++++++++++++++++ .../graphql/servlet/OsgiGraphQLServlet.java | 17 +++++++++++- .../graphql/servlet/SimpleGraphQLServlet.java | 7 +++++ 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/main/java/graphql/servlet/InstrumentationProvider.java create mode 100644 src/main/java/graphql/servlet/NoopInstrumentationProvider.java diff --git a/src/main/java/graphql/servlet/GraphQLServlet.java b/src/main/java/graphql/servlet/GraphQLServlet.java index 5146f01f..05a1d811 100644 --- a/src/main/java/graphql/servlet/GraphQLServlet.java +++ b/src/main/java/graphql/servlet/GraphQLServlet.java @@ -27,6 +27,7 @@ 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; @@ -57,6 +58,8 @@ import java.util.function.Consumer; import java.util.stream.Collectors; +import static graphql.GraphQL.newGraphQL; + /** * @author Andrew Potter */ @@ -72,6 +75,7 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra protected abstract GraphQLContext createContext(Optional request, Optional response); protected abstract ExecutionStrategy getExecutionStrategy(); + protected abstract Instrumentation getInstrumentation(); protected abstract Map transformVariables(GraphQLSchema schema, String query, Map variables); private final List operationListeners; @@ -214,7 +218,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(); diff --git a/src/main/java/graphql/servlet/InstrumentationProvider.java b/src/main/java/graphql/servlet/InstrumentationProvider.java new file mode 100644 index 00000000..176eea0f --- /dev/null +++ b/src/main/java/graphql/servlet/InstrumentationProvider.java @@ -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(); +} diff --git a/src/main/java/graphql/servlet/NoopInstrumentationProvider.java b/src/main/java/graphql/servlet/NoopInstrumentationProvider.java new file mode 100644 index 00000000..67bf4785 --- /dev/null +++ b/src/main/java/graphql/servlet/NoopInstrumentationProvider.java @@ -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; + } +} diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index 6e5e6762..0d348872 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -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; @@ -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; @@ -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 req, Optional resp) { return contextBuilder.build(req, resp); } @@ -147,6 +157,11 @@ protected ExecutionStrategy getExecutionStrategy() { return executionStrategyProvider.getExecutionStrategy(); } + @Override + protected Instrumentation getInstrumentation() { + return instrumentationProvider.getInstrumentation(); + } + @Override protected Map transformVariables(GraphQLSchema schema, String query, Map variables) { return new GraphQLVariables(schema, query, variables); diff --git a/src/main/java/graphql/servlet/SimpleGraphQLServlet.java b/src/main/java/graphql/servlet/SimpleGraphQLServlet.java index 191e7d07..36b1a8ae 100644 --- a/src/main/java/graphql/servlet/SimpleGraphQLServlet.java +++ b/src/main/java/graphql/servlet/SimpleGraphQLServlet.java @@ -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; @@ -69,6 +71,11 @@ protected GraphQLContext createContext(Optional request, Opt return new GraphQLContext(request, response); } + @Override + protected Instrumentation getInstrumentation() { + return NoOpInstrumentation.INSTANCE; + } + @Override protected ExecutionStrategy getExecutionStrategy() { return executionStrategy; From 2d58eac9cf2dd7752bd0c5a1a266f7c30547b6d0 Mon Sep 17 00:00:00 2001 From: Rogers Date: Sun, 7 May 2017 15:17:00 -0400 Subject: [PATCH 2/3] Forgot to add instrumention in query method. Added instrumentation to simple servlet. --- .../java/graphql/servlet/GraphQLServlet.java | 2 +- .../graphql/servlet/SimpleGraphQLServlet.java | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/graphql/servlet/GraphQLServlet.java b/src/main/java/graphql/servlet/GraphQLServlet.java index 05a1d811..d1005db1 100644 --- a/src/main/java/graphql/servlet/GraphQLServlet.java +++ b/src/main/java/graphql/servlet/GraphQLServlet.java @@ -272,7 +272,7 @@ private void query(String query, String operationName, Map 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 errors = executionResult.getErrors(); final Object data = executionResult.getData(); diff --git a/src/main/java/graphql/servlet/SimpleGraphQLServlet.java b/src/main/java/graphql/servlet/SimpleGraphQLServlet.java index 36b1a8ae..47f53eb4 100644 --- a/src/main/java/graphql/servlet/SimpleGraphQLServlet.java +++ b/src/main/java/graphql/servlet/SimpleGraphQLServlet.java @@ -40,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 operationListeners, List servletListeners) { + public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy, List operationListeners, List 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() { @@ -72,13 +79,13 @@ protected GraphQLContext createContext(Optional request, Opt } @Override - protected Instrumentation getInstrumentation() { - return NoOpInstrumentation.INSTANCE; + protected ExecutionStrategy getExecutionStrategy() { + return executionStrategy; } @Override - protected ExecutionStrategy getExecutionStrategy() { - return executionStrategy; + protected Instrumentation getInstrumentation() { + return instrumentation; } @Override From 2e6535da8866f1c710087025379e9b8fbf9e839a Mon Sep 17 00:00:00 2001 From: Rogers Date: Mon, 8 May 2017 10:42:36 -0400 Subject: [PATCH 3/3] Renamed the no-op provider to match the type it provides for. --- src/main/java/graphql/servlet/GraphQLServlet.java | 1 - ...entationProvider.java => NoOpInstrumentationProvider.java} | 2 +- src/main/java/graphql/servlet/OsgiGraphQLServlet.java | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) rename src/main/java/graphql/servlet/{NoopInstrumentationProvider.java => NoOpInstrumentationProvider.java} (92%) diff --git a/src/main/java/graphql/servlet/GraphQLServlet.java b/src/main/java/graphql/servlet/GraphQLServlet.java index d1005db1..9d85d0fa 100644 --- a/src/main/java/graphql/servlet/GraphQLServlet.java +++ b/src/main/java/graphql/servlet/GraphQLServlet.java @@ -23,7 +23,6 @@ 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; diff --git a/src/main/java/graphql/servlet/NoopInstrumentationProvider.java b/src/main/java/graphql/servlet/NoOpInstrumentationProvider.java similarity index 92% rename from src/main/java/graphql/servlet/NoopInstrumentationProvider.java rename to src/main/java/graphql/servlet/NoOpInstrumentationProvider.java index 67bf4785..9ba6c43b 100644 --- a/src/main/java/graphql/servlet/NoopInstrumentationProvider.java +++ b/src/main/java/graphql/servlet/NoOpInstrumentationProvider.java @@ -17,7 +17,7 @@ import graphql.execution.instrumentation.Instrumentation; import graphql.execution.instrumentation.NoOpInstrumentation; -public class NoopInstrumentationProvider implements InstrumentationProvider { +public class NoOpInstrumentationProvider implements InstrumentationProvider { @Override public Instrumentation getInstrumentation() { diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index 0d348872..ccce08a2 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -49,7 +49,7 @@ public class OsgiGraphQLServlet extends GraphQLServlet { private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder(); private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider(); - private InstrumentationProvider instrumentationProvider = new NoopInstrumentationProvider(); + private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider(); private GraphQLSchema schema; private GraphQLSchema readOnlySchema; @@ -145,7 +145,7 @@ public void setInstrumentationProvider(InstrumentationProvider provider) { instrumentationProvider = provider; } public void unsetInstrumentationProvider(ExecutionStrategyProvider provider) { - instrumentationProvider = new NoopInstrumentationProvider(); + instrumentationProvider = new NoOpInstrumentationProvider(); } protected GraphQLContext createContext(Optional req, Optional resp) {