Skip to content

Commit 68a8698

Browse files
authored
Merge pull request #2931 from graphql-java/remove_deprecated_instrumentation_methods_usages
Change Instrumentation production implementations to use non deprecated methods
2 parents 7d20b25 + dbc9896 commit 68a8698

File tree

10 files changed

+86
-107
lines changed

10 files changed

+86
-107
lines changed

src/main/java/graphql/analysis/MaxQueryComplexityInstrumentation.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
1212
import graphql.execution.instrumentation.parameters.InstrumentationValidationParameters;
1313
import graphql.validation.ValidationError;
14+
import org.jetbrains.annotations.Nullable;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617

@@ -26,7 +27,7 @@
2627

2728
/**
2829
* Prevents execution if the query complexity is greater than the specified maxComplexity.
29-
*
30+
* <p>
3031
* Use the {@code Function<QueryComplexityInfo, Boolean>} parameter to supply a function to perform a custom action when the max complexity
3132
* is exceeded. If the function returns {@code true} a {@link AbortExecutionException} is thrown.
3233
*/
@@ -87,25 +88,26 @@ public InstrumentationState createState(InstrumentationCreateStateParameters par
8788
return new State();
8889
}
8990

91+
9092
@Override
91-
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters) {
92-
State state = parameters.getInstrumentationState();
93+
public @Nullable InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters, InstrumentationState rawState) {
94+
State state = (State) rawState;
9395
// for API backwards compatibility reasons we capture the validation parameters, so we can put them into QueryComplexityInfo
9496
state.instrumentationValidationParameters.set(parameters);
9597
return noOp();
9698
}
9799

98100
@Override
99-
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters instrumentationExecuteOperationParameters) {
100-
State state = instrumentationExecuteOperationParameters.getInstrumentationState();
101+
public @Nullable InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters instrumentationExecuteOperationParameters, InstrumentationState rawState) {
102+
State state = (State) rawState;
101103
QueryTraverser queryTraverser = newQueryTraverser(instrumentationExecuteOperationParameters.getExecutionContext());
102104

103105
Map<QueryVisitorFieldEnvironment, Integer> valuesByParent = new LinkedHashMap<>();
104106
queryTraverser.visitPostOrder(new QueryVisitorStub() {
105107
@Override
106108
public void visitField(QueryVisitorFieldEnvironment env) {
107-
int childsComplexity = valuesByParent.getOrDefault(env, 0);
108-
int value = calculateComplexity(env, childsComplexity);
109+
int childComplexity = valuesByParent.getOrDefault(env, 0);
110+
int value = calculateComplexity(env, childComplexity);
109111

110112
valuesByParent.compute(env.getParentEnvironment(), (key, oldValue) ->
111113
ofNullable(oldValue).orElse(0) + value
@@ -136,7 +138,7 @@ public void visitField(QueryVisitorFieldEnvironment env) {
136138
* @param totalComplexity the complexity of the query
137139
* @param maxComplexity the maximum complexity allowed
138140
*
139-
* @return a instance of AbortExecutionException
141+
* @return an instance of AbortExecutionException
140142
*/
141143
protected AbortExecutionException mkAbortException(int totalComplexity, int maxComplexity) {
142144
return new AbortExecutionException("maximum query complexity exceeded " + totalComplexity + " > " + maxComplexity);
@@ -151,12 +153,12 @@ QueryTraverser newQueryTraverser(ExecutionContext executionContext) {
151153
.build();
152154
}
153155

154-
private int calculateComplexity(QueryVisitorFieldEnvironment queryVisitorFieldEnvironment, int childsComplexity) {
156+
private int calculateComplexity(QueryVisitorFieldEnvironment queryVisitorFieldEnvironment, int childComplexity) {
155157
if (queryVisitorFieldEnvironment.isTypeNameIntrospectionField()) {
156158
return 0;
157159
}
158160
FieldComplexityEnvironment fieldComplexityEnvironment = convertEnv(queryVisitorFieldEnvironment);
159-
return fieldComplexityCalculator.calculate(fieldComplexityEnvironment, childsComplexity);
161+
return fieldComplexityCalculator.calculate(fieldComplexityEnvironment, childComplexity);
160162
}
161163

162164
private FieldComplexityEnvironment convertEnv(QueryVisitorFieldEnvironment queryVisitorFieldEnvironment) {

src/main/java/graphql/analysis/MaxQueryDepthInstrumentation.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import graphql.execution.AbortExecutionException;
66
import graphql.execution.ExecutionContext;
77
import graphql.execution.instrumentation.InstrumentationContext;
8+
import graphql.execution.instrumentation.InstrumentationState;
89
import graphql.execution.instrumentation.SimpleInstrumentation;
910
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
11+
import org.jetbrains.annotations.Nullable;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

@@ -16,7 +18,7 @@
1618

1719
/**
1820
* Prevents execution if the query depth is greater than the specified maxDepth.
19-
*
21+
* <p>
2022
* Use the {@code Function<QueryDepthInfo, Boolean>} parameter to supply a function to perform a custom action when the max depth is
2123
* exceeded. If the function returns {@code true} a {@link AbortExecutionException} is thrown.
2224
*/
@@ -49,7 +51,7 @@ public MaxQueryDepthInstrumentation(int maxDepth, Function<QueryDepthInfo, Boole
4951
}
5052

5153
@Override
52-
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
54+
public @Nullable InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters, InstrumentationState state) {
5355
QueryTraverser queryTraverser = newQueryTraverser(parameters.getExecutionContext());
5456
int depth = queryTraverser.reducePreOrder((env, acc) -> Math.max(getPathLength(env.getParentEnvironment()), acc), 0);
5557
if (log.isDebugEnabled()) {
@@ -73,7 +75,7 @@ public InstrumentationContext<ExecutionResult> beginExecuteOperation(Instrumenta
7375
* @param depth the depth of the query
7476
* @param maxDepth the maximum depth allowed
7577
*
76-
* @return a instance of AbortExecutionException
78+
* @return an instance of AbortExecutionException
7779
*/
7880
protected AbortExecutionException mkAbortException(int depth, int maxDepth) {
7981
return new AbortExecutionException("maximum query depth exceeded " + depth + " > " + maxDepth);

src/main/java/graphql/execution/instrumentation/SimpleInstrumentation.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
/**
1818
* An implementation of {@link graphql.execution.instrumentation.Instrumentation} that does nothing. It can be used
19-
* as a base for derived classes where you only implement the methods you want to
19+
* as a base for derived classes where you only implement the methods you want to. With all the methods in {@link Instrumentation}
20+
* now defaulted (post Java 6) this class is really not needed anymore but has been retained for backwards compatibility
21+
* reasons.
2022
*/
2123
@PublicApi
2224
public class SimpleInstrumentation implements Instrumentation {
@@ -26,48 +28,4 @@ public class SimpleInstrumentation implements Instrumentation {
2628
*/
2729
public static final SimpleInstrumentation INSTANCE = new SimpleInstrumentation();
2830

29-
@Override
30-
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
31-
return SimpleInstrumentationContext.noOp();
32-
}
33-
34-
@Override
35-
public InstrumentationContext<Document> beginParse(InstrumentationExecutionParameters parameters) {
36-
return SimpleInstrumentationContext.noOp();
37-
}
38-
39-
@Override
40-
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters) {
41-
return SimpleInstrumentationContext.noOp();
42-
}
43-
44-
@Override
45-
public ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
46-
return new ExecutionStrategyInstrumentationContext() {
47-
@Override
48-
public void onDispatched(CompletableFuture<ExecutionResult> result) {
49-
50-
}
51-
52-
@Override
53-
public void onCompleted(ExecutionResult result, Throwable t) {
54-
55-
}
56-
};
57-
}
58-
59-
@Override
60-
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
61-
return SimpleInstrumentationContext.noOp();
62-
}
63-
64-
@Override
65-
public InstrumentationContext<ExecutionResult> beginField(InstrumentationFieldParameters parameters) {
66-
return SimpleInstrumentationContext.noOp();
67-
}
68-
69-
@Override
70-
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
71-
return SimpleInstrumentationContext.noOp();
72-
}
7331
}

src/main/java/graphql/execution/instrumentation/fieldvalidation/FieldValidationInstrumentation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import graphql.PublicApi;
66
import graphql.execution.AbortExecutionException;
77
import graphql.execution.instrumentation.InstrumentationContext;
8+
import graphql.execution.instrumentation.InstrumentationState;
89
import graphql.execution.instrumentation.SimpleInstrumentation;
910
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
11+
import org.jetbrains.annotations.Nullable;
1012

1113
import java.util.List;
1214

@@ -36,8 +38,7 @@ public FieldValidationInstrumentation(FieldValidation fieldValidation) {
3638
}
3739

3840
@Override
39-
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
40-
41+
public @Nullable InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters, InstrumentationState state) {
4142
List<GraphQLError> errors = FieldValidationSupport.validateFieldsAndArguments(fieldValidation, parameters.getExecutionContext());
4243
if (errors != null && !errors.isEmpty()) {
4344
throw new AbortExecutionException(errors);

src/main/java/graphql/execution/instrumentation/threadpools/ExecutorInstrumentation.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import graphql.Internal;
66
import graphql.TrivialDataFetcher;
77
import graphql.execution.Async;
8+
import graphql.execution.instrumentation.InstrumentationState;
89
import graphql.execution.instrumentation.SimpleInstrumentation;
910
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
1011
import graphql.schema.DataFetcher;
1112
import graphql.schema.DataFetchingEnvironment;
13+
import org.jetbrains.annotations.NotNull;
1214

1315
import java.util.concurrent.CompletableFuture;
1416
import java.util.concurrent.CompletionStage;
@@ -24,13 +26,13 @@
2426
* This instrumentation can be used to control on what thread calls to {@link DataFetcher}s happen on.
2527
* <p>
2628
* If your data fetching is inherently IO bound then you could use a IO oriented thread pool for your fetches and transfer control
27-
* back to a CPU oriented thread pool and allow graphql-java code to run the post processing of results there.
29+
* back to a CPU oriented thread pool and allow graphql-java code to run the post-processing of results there.
2830
* <p>
2931
* An IO oriented thread pool is typically a multiple of {@link Runtime#availableProcessors()} while a CPU oriented thread pool
3032
* is typically no more than {@link Runtime#availableProcessors()}.
3133
* <p>
32-
* The instrumentation will use the {@link graphql.execution.instrumentation.Instrumentation#instrumentDataFetcher(DataFetcher, InstrumentationFieldFetchParameters)}
33-
* method to change your data fetchers so they are executed on a thread pool dedicated to fetching (if you provide one).
34+
* The instrumentation will use the {@link graphql.execution.instrumentation.Instrumentation#instrumentDataFetcher(DataFetcher, InstrumentationFieldFetchParameters, InstrumentationState)}
35+
* method to change your data fetchers, so they are executed on a thread pool dedicated to fetching (if you provide one).
3436
* <p>
3537
* Once the data fetcher value is returns it will transfer control back to a processing thread pool (if you provide one).
3638
* <p>
@@ -106,7 +108,7 @@ public ExecutorInstrumentation build() {
106108
}
107109

108110
@Override
109-
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters) {
111+
public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
110112
if (originalDataFetcher instanceof TrivialDataFetcher) {
111113
return originalDataFetcher;
112114
}
@@ -117,7 +119,7 @@ public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher,
117119
// the CF will be left running on that fetch executors thread
118120
invokedCF = CompletableFuture.supplyAsync(invokedAsync(originalDataFetcher, environment), fetchExecutor);
119121
} else {
120-
invokedCF = invokedSynch(originalDataFetcher, environment);
122+
invokedCF = invokedSync(originalDataFetcher, environment);
121123
}
122124
if (processingExecutor != null) {
123125
invokedCF = invokedCF.thenApplyAsync(processingControl(), processingExecutor);
@@ -136,7 +138,7 @@ private Supplier<CompletionStage<?>> invokedAsync(DataFetcher<?> originalDataFet
136138
};
137139
}
138140

139-
private CompletableFuture<CompletionStage<?>> invokedSynch(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment) {
141+
private CompletableFuture<CompletionStage<?>> invokedSync(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment) {
140142
actionObserver.accept(FETCHING);
141143
return CompletableFuture.completedFuture(invokeOriginalDF(originalDataFetcher, environment));
142144
}

src/main/java/graphql/execution/instrumentation/tracing/TracingInstrumentation.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import graphql.execution.instrumentation.InstrumentationContext;
99
import graphql.execution.instrumentation.InstrumentationState;
1010
import graphql.execution.instrumentation.SimpleInstrumentation;
11+
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters;
1112
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
1213
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
1314
import graphql.execution.instrumentation.parameters.InstrumentationValidationParameters;
1415
import graphql.language.Document;
1516
import graphql.validation.ValidationError;
17+
import org.jetbrains.annotations.Nullable;
1618

1719
import java.util.Collections;
1820
import java.util.LinkedHashMap;
@@ -69,38 +71,38 @@ public TracingInstrumentation(Options options) {
6971
private final Options options;
7072

7173
@Override
72-
public InstrumentationState createState() {
74+
public @Nullable InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
7375
return new TracingSupport(options.includeTrivialDataFetchers);
7476
}
7577

7678
@Override
77-
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
79+
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters, InstrumentationState rawState) {
7880
Map<Object, Object> currentExt = executionResult.getExtensions();
7981

80-
TracingSupport tracingSupport = parameters.getInstrumentationState();
82+
TracingSupport tracingSupport = (TracingSupport) rawState;
8183
Map<Object, Object> withTracingExt = new LinkedHashMap<>(currentExt == null ? ImmutableKit.emptyMap() : currentExt);
8284
withTracingExt.put("tracing", tracingSupport.snapshotTracingData());
8385

8486
return CompletableFuture.completedFuture(new ExecutionResultImpl(executionResult.getData(), executionResult.getErrors(), withTracingExt));
8587
}
8688

8789
@Override
88-
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
89-
TracingSupport tracingSupport = parameters.getInstrumentationState();
90+
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters, InstrumentationState rawState) {
91+
TracingSupport tracingSupport = (TracingSupport) rawState;
9092
TracingSupport.TracingContext ctx = tracingSupport.beginField(parameters.getEnvironment(), parameters.isTrivialDataFetcher());
9193
return whenCompleted((result, t) -> ctx.onEnd());
9294
}
9395

9496
@Override
95-
public InstrumentationContext<Document> beginParse(InstrumentationExecutionParameters parameters) {
96-
TracingSupport tracingSupport = parameters.getInstrumentationState();
97+
public InstrumentationContext<Document> beginParse(InstrumentationExecutionParameters parameters,InstrumentationState rawState) {
98+
TracingSupport tracingSupport = (TracingSupport) rawState;
9799
TracingSupport.TracingContext ctx = tracingSupport.beginParse();
98100
return whenCompleted((result, t) -> ctx.onEnd());
99101
}
100102

101103
@Override
102-
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters) {
103-
TracingSupport tracingSupport = parameters.getInstrumentationState();
104+
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters, InstrumentationState rawState) {
105+
TracingSupport tracingSupport = (TracingSupport) rawState;
104106
TracingSupport.TracingContext ctx = tracingSupport.beginValidation();
105107
return whenCompleted((result, t) -> ctx.onEnd());
106108
}

0 commit comments

Comments
 (0)