Max query depth called later in beginExecuteOperation by bbakerman · Pull Request #2773 · 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
52 changes: 24 additions & 28 deletions src/main/java/graphql/analysis/MaxQueryDepthInstrumentation.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package graphql.analysis;

import graphql.ExecutionResult;
import graphql.PublicApi;
import graphql.execution.AbortExecutionException;
import graphql.execution.ExecutionContext;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationValidationParameters;
import graphql.validation.ValidationError;
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.function.Function;

import static graphql.execution.instrumentation.SimpleInstrumentationContext.whenCompleted;
import static graphql.execution.instrumentation.SimpleInstrumentationContext.noOp;

/**
* Prevents execution if the query depth is greater than the specified maxDepth.
Expand Down Expand Up @@ -49,26 +49,22 @@ public MaxQueryDepthInstrumentation(int maxDepth, Function<QueryDepthInfo, Boole
}

@Override
public InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters) {
return whenCompleted((errors, throwable) -> {
if ((errors != null && errors.size() > 0) || throwable != null) {
return;
}
QueryTraverser queryTraverser = newQueryTraverser(parameters);
int depth = queryTraverser.reducePreOrder((env, acc) -> Math.max(getPathLength(env.getParentEnvironment()), acc), 0);
if (log.isDebugEnabled()) {
log.debug("Query depth info: {}", depth);
}
if (depth > maxDepth) {
QueryDepthInfo queryDepthInfo = QueryDepthInfo.newQueryDepthInfo()
.depth(depth)
.build();
boolean throwAbortException = maxQueryDepthExceededFunction.apply(queryDepthInfo);
if (throwAbortException) {
throw mkAbortException(depth, maxDepth);
}
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
QueryTraverser queryTraverser = newQueryTraverser(parameters.getExecutionContext());
int depth = queryTraverser.reducePreOrder((env, acc) -> Math.max(getPathLength(env.getParentEnvironment()), acc), 0);
if (log.isDebugEnabled()) {
log.debug("Query depth info: {}", depth);
}
if (depth > maxDepth) {
QueryDepthInfo queryDepthInfo = QueryDepthInfo.newQueryDepthInfo()
.depth(depth)
.build();
boolean throwAbortException = maxQueryDepthExceededFunction.apply(queryDepthInfo);
if (throwAbortException) {
throw mkAbortException(depth, maxDepth);
}
});
}
return noOp();
}

/**
Expand All @@ -83,12 +79,12 @@ protected AbortExecutionException mkAbortException(int depth, int maxDepth) {
return new AbortExecutionException("maximum query depth exceeded " + depth + " > " + maxDepth);
}

QueryTraverser newQueryTraverser(InstrumentationValidationParameters parameters) {
QueryTraverser newQueryTraverser(ExecutionContext executionContext) {
return QueryTraverser.newQueryTraverser()
.schema(parameters.getSchema())
.document(parameters.getDocument())
.operationName(parameters.getOperation())
.variables(parameters.getVariables())
.schema(executionContext.getGraphQLSchema())
.document(executionContext.getDocument())
.operationName(executionContext.getExecutionInput().getOperationName())
.variables(executionContext.getVariables())
.build();
}

Expand Down
30 changes: 25 additions & 5 deletions src/main/java/graphql/analysis/QueryComplexityInfo.java
Loading