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
82 changes: 82 additions & 0 deletions src/jmh/java/benchmark/InputCoercionBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package benchmark;

import graphql.GraphQLContext;
import graphql.execution.CoercedVariables;
import graphql.execution.RawVariables;
import graphql.execution.ValuesResolver;
import graphql.language.TypeName;
import graphql.language.VariableDefinition;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.SchemaGenerator;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 3, time = 5)
@Fork(2)
public class InputCoercionBenchmark {

@Param({"scalar", "shallowObject", "flatList", "nestedObject"})
public String scenario;

private GraphQLSchema schema;
private List<VariableDefinition> definitions;
private RawVariables variables;
private GraphQLContext context;

@Setup
public void setup() {
schema = SchemaGenerator.createdMockedSchema("type Query { echo(arg: Recursive): Int } " +
"input Recursive { next: Recursive items: [Int] }");
boolean scalar = "scalar".equals(scenario);
definitions = Collections.singletonList(new VariableDefinition("v", new TypeName(scalar ? "Int" : "Recursive")));
variables = RawVariables.of(Collections.singletonMap("v", inputValue()));
context = GraphQLContext.getDefault();
}

@Benchmark
public CoercedVariables coerce() {
return ValuesResolver.coerceVariableValues(schema, definitions, variables, context, Locale.ROOT);
}

private Object inputValue() {
if ("scalar".equals(scenario)) {
return 42;
}
if ("shallowObject".equals(scenario)) {
return Collections.emptyMap();
}
if ("flatList".equals(scenario)) {
List<Integer> items = new ArrayList<>();
for (int i = 0; i < 100; i++) {
items.add(i);
}
return Collections.singletonMap("items", items);
}
Map<String, Object> value = new LinkedHashMap<>();
for (int i = 0; i < 32; i++) {
value = Collections.singletonMap("next", value);
}
return value;
}
}
114 changes: 114 additions & 0 deletions src/main/java/graphql/execution/InputValueLimits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package graphql.execution;

import graphql.PublicApi;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;

import java.util.function.Consumer;

/**
* Limits the nesting of supplied variable values and schema default values. Each list or input object value
* on a path increases the depth by one. Sibling values do not add to each other's depth,
* Non-Null wrappers do not count, and scalar values are not traversed. A value coerced to a
* singleton list counts as one list level.
*
* <p>Schema defaults, whether SDL literals or programmatic values, are checked independently
* when the schema is validated, using the current JVM-wide limit. Defaults are not checked
* again against request overrides when they are used or exposed through introspection.
* Scalar payloads and defaults filled in for omitted fields do not add to the supplied value's depth.
* Programmatic list defaults must use collections or arrays; iterators, streams and other iterables
* are rejected during schema validation without being consumed.
*
* <p>Put an instance under {@link #KEY} in {@code ExecutionInput.getGraphQLContext()} to
* override the JVM-wide default of 128 for supplied variable values in a request.
* Use {@link #NONE} to disable the limit.
* <pre>{@code
* InputValueLimits limits = InputValueLimits.newInputValueLimits()
* .maxDepth(64)
* .build();
* executionInput.getGraphQLContext().put(InputValueLimits.KEY, limits);
* }</pre>
*/
@PublicApi
@NullMarked
public class InputValueLimits {

public static final int DEFAULT_MAX_DEPTH = 128;
public static final String KEY = "graphql.execution.InputValueLimits";
public static final InputValueLimits DEFAULT = new InputValueLimits(DEFAULT_MAX_DEPTH);
public static final InputValueLimits NONE = new InputValueLimits(Integer.MAX_VALUE);

private static volatile InputValueLimits defaultLimits = DEFAULT;

private final int maxDepth;

private InputValueLimits(int maxDepth) {
this.maxDepth = maxDepth;
}

/**
* Sets the limit used for subsequent schema validation and for requests without an override.
* This does not revalidate existing schemas.
*/
public static void setDefaultLimits(InputValueLimits limits) {
defaultLimits = limits;
}

/**
* Returns the JVM-wide default.
*/
public static InputValueLimits getDefaultLimits() {
return defaultLimits;
}

/**
* Creates a builder initialized from the current JVM-wide default.
*/
public static Builder newInputValueLimits() {
return new Builder(getDefaultLimits());
}

/**
* Returns the maximum number of nested list and input object values on one path.
*/
public int getMaxDepth() {
return maxDepth;
}

/**
* Creates a copy with the changes made by the builder consumer.
*/
public InputValueLimits transform(Consumer<Builder> builderConsumer) {
Builder builder = new Builder(this);
builderConsumer.accept(builder);
return builder.build();
}

@PublicApi
@NullUnmarked
public static class Builder {
private int maxDepth;

private Builder(InputValueLimits limits) {
this.maxDepth = limits.maxDepth;
}

/**
* Sets the maximum depth. The value must be positive.
*/
public Builder maxDepth(int maxDepth) {
if (maxDepth <= 0) {
throw new IllegalArgumentException("maxDepth must be positive");
}
this.maxDepth = maxDepth;
return this;
}

/**
* Creates the limits instance.
*/
public InputValueLimits build() {
return new InputValueLimits(maxDepth);
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public static CoercedVariables coerceVariableValues(GraphQLSchema schema,


/**
* Normalized variables values are Literals with type information. No validation here!
* Normalized variable values are Literals with type information. Variable values are assumed
* to be valid, but input value depth is still limited.
*
* @param schema the schema to use
* @param variableDefinitions the list of variable definitions
Expand Down Expand Up @@ -375,7 +376,7 @@ private static Map<String, Object> getArgumentValuesImpl(
coercedValues.put(argumentName, value);
}

ValuesResolverOneOfValidation.validateOneOfInputTypes(argumentType, value, argumentValue, argumentName, locale);
ValuesResolverOneOfValidation.validateOneOfInputTypes(argumentType, value, argumentValue, locale);

}
}
Expand Down
Loading
Loading