Change Instrumentation production implementations to use non deprecated methods by bbakerman · Pull Request #2931 · graphql-java/graphql-java · GitHub
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import graphql.execution.AbortExecutionException;
import graphql.execution.ExecutionContext;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -16,7 +18,7 @@

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

@Override
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
public @Nullable InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters, InstrumentationState state) {
QueryTraverser queryTraverser = newQueryTraverser(parameters.getExecutionContext());
int depth = queryTraverser.reducePreOrder((env, acc) -> Math.max(getPathLength(env.getParentEnvironment()), acc), 0);
if (log.isDebugEnabled()) {
Expand All @@ -73,7 +75,7 @@ public InstrumentationContext<ExecutionResult> beginExecuteOperation(Instrumenta
* @param depth the depth of the query
* @param maxDepth the maximum depth allowed
*
* @return a instance of AbortExecutionException
* @return an instance of AbortExecutionException
*/
protected AbortExecutionException mkAbortException(int depth, int maxDepth) {
return new AbortExecutionException("maximum query depth exceeded " + depth + " > " + maxDepth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

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

@Override
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
return SimpleInstrumentationContext.noOp();
}

@Override
public InstrumentationContext<Document> beginParse(InstrumentationExecutionParameters parameters) {
return SimpleInstrumentationContext.noOp();
}

@Override
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters) {
return SimpleInstrumentationContext.noOp();
}

@Override
public ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
return new ExecutionStrategyInstrumentationContext() {
@Override
public void onDispatched(CompletableFuture<ExecutionResult> result) {

}

@Override
public void onCompleted(ExecutionResult result, Throwable t) {

}
};
}

@Override
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
return SimpleInstrumentationContext.noOp();
}

@Override
public InstrumentationContext<ExecutionResult> beginField(InstrumentationFieldParameters parameters) {
return SimpleInstrumentationContext.noOp();
}

@Override
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
return SimpleInstrumentationContext.noOp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import graphql.PublicApi;
import graphql.execution.AbortExecutionException;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
import org.jetbrains.annotations.Nullable;

import java.util.List;

Expand Down Expand Up @@ -36,8 +38,7 @@ public FieldValidationInstrumentation(FieldValidation fieldValidation) {
}

@Override
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {

public @Nullable InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters, InstrumentationState state) {
List<GraphQLError> errors = FieldValidationSupport.validateFieldsAndArguments(fieldValidation, parameters.getExecutionContext());
if (errors != null && !errors.isEmpty()) {
throw new AbortExecutionException(errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import graphql.Internal;
import graphql.TrivialDataFetcher;
import graphql.execution.Async;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.jetbrains.annotations.NotNull;

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

@Override
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters) {
public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
if (originalDataFetcher instanceof TrivialDataFetcher) {
return originalDataFetcher;
}
Expand All @@ -117,7 +119,7 @@ public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher,
// the CF will be left running on that fetch executors thread
invokedCF = CompletableFuture.supplyAsync(invokedAsync(originalDataFetcher, environment), fetchExecutor);
} else {
invokedCF = invokedSynch(originalDataFetcher, environment);
invokedCF = invokedSync(originalDataFetcher, environment);
}
if (processingExecutor != null) {
invokedCF = invokedCF.thenApplyAsync(processingControl(), processingExecutor);
Expand All @@ -136,7 +138,7 @@ private Supplier<CompletionStage<?>> invokedAsync(DataFetcher<?> originalDataFet
};
}

private CompletableFuture<CompletionStage<?>> invokedSynch(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment) {
private CompletableFuture<CompletionStage<?>> invokedSync(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment) {
actionObserver.accept(FETCHING);
return CompletableFuture.completedFuture(invokeOriginalDF(originalDataFetcher, environment));
}
Expand Down
Loading