Skip to content

Commit 4535e66

Browse files
ronald-d-rogersapottere
authored andcommitted
Added support for instrumentation (#27)
* Added support for instrumentation * Forgot to add instrumention in query method. Added instrumentation to simple servlet. * Renamed the no-op provider to match the type it provides for.
1 parent d31ac83 commit 4535e66

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

src/main/java/graphql/servlet/GraphQLServlet.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2424
import com.google.common.io.CharStreams;
2525
import graphql.ExecutionResult;
26-
import graphql.GraphQL;
2726
import graphql.GraphQLError;
2827
import graphql.InvalidSyntaxError;
2928
import graphql.execution.ExecutionStrategy;
29+
import graphql.execution.instrumentation.Instrumentation;
3030
import graphql.schema.GraphQLFieldDefinition;
3131
import graphql.schema.GraphQLSchema;
3232
import graphql.validation.ValidationError;
@@ -57,6 +57,8 @@
5757
import java.util.function.Consumer;
5858
import java.util.stream.Collectors;
5959

60+
import static graphql.GraphQL.newGraphQL;
61+
6062
/**
6163
* @author Andrew Potter
6264
*/
@@ -72,6 +74,7 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra
7274

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

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.servlet;
16+
17+
import graphql.execution.instrumentation.Instrumentation;
18+
19+
public interface InstrumentationProvider {
20+
Instrumentation getInstrumentation();
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.servlet;
16+
17+
import graphql.execution.instrumentation.Instrumentation;
18+
import graphql.execution.instrumentation.NoOpInstrumentation;
19+
20+
public class NoOpInstrumentationProvider implements InstrumentationProvider {
21+
22+
@Override
23+
public Instrumentation getInstrumentation() {
24+
return NoOpInstrumentation.INSTANCE;
25+
}
26+
}

src/main/java/graphql/servlet/OsgiGraphQLServlet.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package graphql.servlet;
1616

1717
import graphql.execution.ExecutionStrategy;
18+
import graphql.execution.instrumentation.Instrumentation;
1819
import graphql.schema.GraphQLFieldDefinition;
1920
import graphql.schema.GraphQLObjectType;
2021
import graphql.schema.GraphQLSchema;
@@ -48,7 +49,8 @@ public class OsgiGraphQLServlet extends GraphQLServlet {
4849

4950
private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
5051
private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider();
51-
52+
private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider();
53+
5254
private GraphQLSchema schema;
5355
private GraphQLSchema readOnlySchema;
5456

@@ -138,6 +140,14 @@ public void unsetExecutionStrategyProvider(ExecutionStrategyProvider provider) {
138140
executionStrategyProvider = new EnhancedExecutionStrategyProvider();
139141
}
140142

143+
@Reference(cardinality = ReferenceCardinality.OPTIONAL)
144+
public void setInstrumentationProvider(InstrumentationProvider provider) {
145+
instrumentationProvider = provider;
146+
}
147+
public void unsetInstrumentationProvider(ExecutionStrategyProvider provider) {
148+
instrumentationProvider = new NoOpInstrumentationProvider();
149+
}
150+
141151
protected GraphQLContext createContext(Optional<HttpServletRequest> req, Optional<HttpServletResponse> resp) {
142152
return contextBuilder.build(req, resp);
143153
}
@@ -147,6 +157,11 @@ protected ExecutionStrategy getExecutionStrategy() {
147157
return executionStrategyProvider.getExecutionStrategy();
148158
}
149159

160+
@Override
161+
protected Instrumentation getInstrumentation() {
162+
return instrumentationProvider.getInstrumentation();
163+
}
164+
150165
@Override
151166
protected Map<String, Object> transformVariables(GraphQLSchema schema, String query, Map<String, Object> variables) {
152167
return new GraphQLVariables(schema, query, variables);

src/main/java/graphql/servlet/SimpleGraphQLServlet.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package graphql.servlet;
1616

1717
import graphql.execution.ExecutionStrategy;
18+
import graphql.execution.instrumentation.Instrumentation;
19+
import graphql.execution.instrumentation.NoOpInstrumentation;
1820
import graphql.schema.GraphQLObjectType;
1921
import graphql.schema.GraphQLSchema;
2022

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

4042
public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy) {
41-
this(schema, executionStrategy, null, null);
43+
this(schema, executionStrategy, null, null, null);
4244
}
4345

44-
public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy, List<GraphQLOperationListener> operationListeners, List<GraphQLServletListener> servletListeners) {
46+
public SimpleGraphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy, List<GraphQLOperationListener> operationListeners, List<GraphQLServletListener> servletListeners, Instrumentation instrumentation) {
4547
super(operationListeners, servletListeners, null);
4648

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

5052
this.executionStrategy = executionStrategy;
53+
54+
if (instrumentation == null) {
55+
this.instrumentation = NoOpInstrumentation.INSTANCE;
56+
} else {
57+
this.instrumentation = instrumentation;
58+
}
5159
}
5260

5361
private final GraphQLSchema schema;
5462
private final GraphQLSchema readOnlySchema;
5563
private final ExecutionStrategy executionStrategy;
64+
private final Instrumentation instrumentation;
5665

5766
@Override
5867
public GraphQLSchema getSchema() {
@@ -74,6 +83,11 @@ protected ExecutionStrategy getExecutionStrategy() {
7483
return executionStrategy;
7584
}
7685

86+
@Override
87+
protected Instrumentation getInstrumentation() {
88+
return instrumentation;
89+
}
90+
7791
@Override
7892
protected Map<String, Object> transformVariables(GraphQLSchema schema, String query, Map<String, Object> variables) {
7993
return variables;

0 commit comments

Comments
 (0)