diff --git a/src/jmh/java/benchmark/VariableBatchBenchmark.java b/src/jmh/java/benchmark/VariableBatchBenchmark.java new file mode 100644 index 0000000000..db6097cfd0 --- /dev/null +++ b/src/jmh/java/benchmark/VariableBatchBenchmark.java @@ -0,0 +1,107 @@ +package benchmark; + +import graphql.ExecutionInput; +import graphql.ExecutionResult; +import graphql.GraphQL; +import graphql.VariableBatchExecutionResult; +import graphql.execution.preparsed.PreparsedDocumentEntry; +import graphql.schema.DataFetcher; +import graphql.schema.idl.RuntimeWiring; +import graphql.schema.idl.SchemaGenerator; +import graphql.schema.idl.SchemaParser; +import org.dataloader.BatchLoader; +import org.dataloader.DataLoaderFactory; +import org.dataloader.DataLoaderRegistry; +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.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +/** Compares one variable batch with repeated ordinary executions sharing the same registry. */ +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@Fork(2) +public class VariableBatchBenchmark { + private static final String QUERY = "query($id:Int!) { a:value(id:$id) b:value(id:$id) c:value(id:$id) }"; + + @Param({"16"}) + public int size; + + @Param({"false", "true"}) + public boolean cachedDocument; + + @Param({"false", "true"}) + public boolean dataLoader; + + private GraphQL graphQL; + private List> variables; + + @Setup + public void setup() { + DataFetcher fetcher = environment -> dataLoader + ? environment.getDataLoader("values").load(environment.getArgument("id")) + : environment.getArgument("id"); + var schema = new SchemaGenerator().makeExecutableSchema( + new SchemaParser().parse("type Query { value(id:Int!):Int! }"), + RuntimeWiring.newRuntimeWiring().type("Query", type -> type.dataFetcher("value", fetcher)).build()); + GraphQL.Builder builder = GraphQL.newGraphQL(schema); + if (cachedDocument) { + AtomicReference prepared = new AtomicReference<>(); + builder.preparsedDocumentProvider((input, compute) -> CompletableFuture.completedFuture( + prepared.updateAndGet(value -> value == null ? compute.apply(input) : value))); + } + graphQL = builder.build(); + variables = new ArrayList<>(); + for (int index = 0; index < size; index++) { + variables.add(Map.of("id", index % 4)); + } + } + + @Benchmark + public List variableBatch() { + ExecutionInput input = ExecutionInput.newExecutionInput(QUERY) + .variables(variables).dataLoaderRegistry(registry()).build(); + VariableBatchExecutionResult result = (VariableBatchExecutionResult) graphQL.execute(input); + return result.getResultsAsync().join(); + } + + @Benchmark + public List separateExecutions() { + DataLoaderRegistry registry = registry(); + List> results = new ArrayList<>(); + for (Map values : variables) { + results.add(graphQL.executeAsync(ExecutionInput.newExecutionInput(QUERY) + .variables(values).dataLoaderRegistry(registry).build())); + } + CompletableFuture.allOf(results.toArray(new CompletableFuture[0])).join(); + List completed = new ArrayList<>(); + results.forEach(result -> completed.add(result.join())); + return completed; + } + + private DataLoaderRegistry registry() { + DataLoaderRegistry registry = new DataLoaderRegistry(); + if (dataLoader) { + BatchLoader loader = keys -> CompletableFuture.completedFuture(keys); + registry.register("values", DataLoaderFactory.newDataLoader(loader)); + } + return registry; + } +} diff --git a/src/main/java/graphql/ExecutionInput.java b/src/main/java/graphql/ExecutionInput.java index a83c741e7f..a0e86fdbe0 100644 --- a/src/main/java/graphql/ExecutionInput.java +++ b/src/main/java/graphql/ExecutionInput.java @@ -4,11 +4,13 @@ import graphql.execution.ExecutionId; import graphql.execution.RawVariables; import graphql.execution.preparsed.persisted.PersistedQuerySupport; +import graphql.extensions.ExtensionsBuilder; import org.dataloader.DataLoaderRegistry; import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.NullUnmarked; import org.jspecify.annotations.Nullable; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -24,13 +26,16 @@ @PublicApi @NullMarked public class ExecutionInput { + private static final List EMPTY_VARIABLES_LIST = List.of(RawVariables.emptyVariables()); private final String query; private final String operationName; private final @Nullable Object context; private final GraphQLContext graphQLContext; private final @Nullable Object localContext; private final Object root; - private final RawVariables rawVariables; + private final List rawVariablesList; + private final @Nullable Integer variableIndex; + private final @Nullable ExecutionId variableBatchId; private final Map extensions; private final DataLoaderRegistry dataLoaderRegistry; private final ExecutionId executionId; @@ -55,7 +60,9 @@ private ExecutionInput(Builder builder) { this.context = builder.context; this.graphQLContext = assertNotNull(builder.graphQLContext); this.root = builder.root; - this.rawVariables = builder.rawVariables; + this.rawVariablesList = builder.rawVariablesList; + this.variableIndex = builder.variableIndex; + this.variableBatchId = builder.variableBatchId; this.dataLoaderRegistry = builder.dataLoaderRegistry; this.executionId = builder.executionId; this.locale = builder.locale != null ? builder.locale : Locale.getDefault(); // always have a locale in place @@ -143,16 +150,74 @@ public Object getRoot() { /** * @return a map of raw variables that can be referenced via $syntax in the query. + * @throws AssertException if more than one variable map is present; use {@link #getVariablesList()} instead */ public Map getVariables() { - return rawVariables.toMap(); + return getRawVariables().toMap(); } /** * @return a map of raw variables that can be referenced via $syntax in the query. + * @throws AssertException if more than one variable map is present; use {@link #getVariablesList()} instead */ public RawVariables getRawVariables() { - return rawVariables; + Assert.assertTrue(rawVariablesList.size() <= 1, + "getRawVariables() requires at most one variable map; use getVariablesList() for variable batches"); + return rawVariablesList.isEmpty() ? RawVariables.emptyVariables() : rawVariablesList.get(0); + } + + /** + * Returns the variable maps in input order. An empty list or a singleton uses ordinary execution; + * a larger list executes the selected operation once per map, sharing the DataLoader registry. + * The {@link #getVariables()} and {@link #getRawVariables()} accessors require at most one map, + * and return an empty map when the list is empty. + * + * @return the immutable list of immutable variable maps (values can be null) + */ + @ExperimentalApi + public List> getVariablesList() { + return ImmutableKit.map(rawVariablesList, RawVariables::toMap); + } + + /** @return whether this input requests multiple executions */ + @ExperimentalApi + public boolean isVariableBatch() { + return rawVariablesList.size() > 1; + } + + /** @return the zero-based index for an individual batch execution, otherwise null */ + @ExperimentalApi + public @Nullable Integer getVariableIndex() { + return variableIndex; + } + + /** @return the shared batch id for an individual batch execution, otherwise null */ + @ExperimentalApi + public @Nullable ExecutionId getVariableBatchId() { + return variableBatchId; + } + + @Internal + public ExecutionInput newVariableBatchEntry(int index, ExecutionId batchId) { + GraphQLContext entryContext = GraphQLContext.newContext().build().putAll(graphQLContext); + ExtensionsBuilder extensionsBuilder = graphQLContext.get(ExtensionsBuilder.class); + if (extensionsBuilder != null) { + entryContext.put(ExtensionsBuilder.class, extensionsBuilder.copy()); + } + Builder builder = newExecutionInput(query) + .operationName(operationName) + .internalTransferContext(entryContext) + .context(context == graphQLContext ? entryContext : context) + .localContext(localContext) + .root(root) + .variables(rawVariablesList.get(index).toMap()) + .extensions(extensions) + .dataLoaderRegistry(dataLoaderRegistry) + .locale(locale) + .profileExecution(profileExecution); + builder.variableIndex = index; + builder.variableBatchId = batchId; + return builder.build(); } /** @@ -257,10 +322,12 @@ public ExecutionInput transform(Consumer builderConsumer) { .localContext(this.localContext) .root(this.root) .dataLoaderRegistry(this.dataLoaderRegistry) - .variables(this.rawVariables.toMap()) .extensions(this.extensions) .executionId(this.executionId) .locale(this.locale); + builder.rawVariablesList = this.rawVariablesList; + builder.variableIndex = this.variableIndex; + builder.variableBatchId = this.variableBatchId; builderConsumer.accept(builder); @@ -275,7 +342,7 @@ public String toString() { ", context=" + context + ", graphQLContext=" + graphQLContext + ", root=" + root + - ", rawVariables=" + rawVariables + + ", rawVariablesList=" + rawVariablesList + ", dataLoaderRegistry=" + dataLoaderRegistry + ", executionId= " + executionId + ", locale= " + locale + @@ -309,7 +376,9 @@ public static class Builder { private Object context = graphQLContext; // we make these the same object on purpose - legacy code will get the same object if this change nothing private Object localContext; private Object root; - private RawVariables rawVariables = RawVariables.emptyVariables(); + private List rawVariablesList = EMPTY_VARIABLES_LIST; + private Integer variableIndex; + private ExecutionId variableBatchId; private Map extensions = ImmutableKit.emptyMap(); // // this is important - it allows code to later known if we never really set a dataloader and hence it can optimize @@ -444,7 +513,23 @@ public Builder root(Object root) { */ public Builder variables(Map rawVariables) { assertNotNull(rawVariables, "variables map can't be null"); - this.rawVariables = RawVariables.of(rawVariables); + this.rawVariablesList = List.of(RawVariables.of(rawVariables)); + return this; + } + + /** + * Supplies complete variable maps for the same selected operation. An empty list executes once + * with empty variables; a singleton executes normally. Larger lists return a + * {@link VariableBatchExecutionResult}. Entries are never deduplicated. Subscriptions cannot be batched. + * + * @param variables the maps in input order; neither the list nor its entries may be null + * @return this builder + */ + @ExperimentalApi + public Builder variables(List> variables) { + assertNotNull(variables, "variables list can't be null"); + this.rawVariablesList = List.copyOf(ImmutableKit.map(variables, + value -> RawVariables.of(assertNotNull(value, "variables list entry can't be null")))); return this; } @@ -456,7 +541,9 @@ public Builder extensions(Map extensions) { /** * You should create new {@link org.dataloader.DataLoaderRegistry}s and new {@link org.dataloader.DataLoader}s for each execution. Do not * re-use - * instances as this will create unexpected results. + * instances across independent requests as this will create unexpected results. Variable batching + * shares the supplied registry and its caches across all entries in that batch, including mutations. + * Mutation resolvers should clear or prime affected cache entries as usual. * * @param dataLoaderRegistry a registry of {@link org.dataloader.DataLoader}s * @@ -476,4 +563,4 @@ public ExecutionInput build() { return new ExecutionInput(this); } } -} \ No newline at end of file +} diff --git a/src/main/java/graphql/GraphQL.java b/src/main/java/graphql/GraphQL.java index d25f596175..a58f5c5481 100644 --- a/src/main/java/graphql/GraphQL.java +++ b/src/main/java/graphql/GraphQL.java @@ -11,6 +11,7 @@ import graphql.execution.ExecutionStrategy; import graphql.execution.SimpleDataFetcherExceptionHandler; import graphql.execution.SubscriptionExecutionStrategy; +import graphql.execution.UnknownOperationException; import graphql.execution.ValueUnboxer; import graphql.execution.instrumentation.DocumentAndVariables; import graphql.execution.instrumentation.Instrumentation; @@ -25,6 +26,8 @@ import graphql.execution.preparsed.PreparsedDocumentProvider; import graphql.introspection.GoodFaithIntrospection; import graphql.language.Document; +import graphql.language.NodeUtil; +import graphql.language.OperationDefinition; import graphql.schema.GraphQLSchema; import graphql.validation.GoodFaithIntrospectionExceeded; import graphql.validation.OperationValidationRule; @@ -32,7 +35,9 @@ import graphql.validation.ValidationError; import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.NullUnmarked; +import org.jspecify.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.concurrent.CompletableFuture; @@ -478,12 +483,16 @@ public CompletableFuture executeAsync(UnaryOperator * This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult} * which is the result of executing the provided query. - * + * Multiple variable maps start one execution per map immediately and return a {@link VariableBatchExecutionResult} + * containing their futures in input order. Batched subscription operations fail with {@link IllegalArgumentException}. * @param executionInput {@link ExecutionInput} * * @return a promise to an {@link ExecutionResult} which can include errors */ public CompletableFuture executeAsync(ExecutionInput executionInput) { + if (executionInput.isVariableBatch()) { + return executeVariableBatch(executionInput); + } Profiler profiler = executionInput.isProfileExecution() ? new ProfilerImpl(executionInput.getGraphQLContext()) : Profiler.NO_OP; EngineRunningState engineRunningState = new EngineRunningState(executionInput, profiler); return engineRunningState.engineRun(() -> { @@ -505,7 +514,7 @@ public CompletableFuture executeAsync(ExecutionInput executionI GraphQLSchema graphQLSchema = instrumentation.instrumentSchema(this.graphQLSchema, instrumentationParameters, instrumentationState); - CompletableFuture executionResult = parseValidateAndExecute(instrumentedExecutionInput, graphQLSchema, instrumentationState, engineRunningState, profiler); + CompletableFuture executionResult = parseValidateAndExecute(instrumentedExecutionInput, graphQLSchema, instrumentationState, engineRunningState, profiler, executionInput.getVariableIndex() != null); // // finish up instrumentation executionResult = executionResult.whenComplete(completeInstrumentationCtxCF(executionInstrumentation)); @@ -520,6 +529,60 @@ public CompletableFuture executeAsync(ExecutionInput executionI }); } + private CompletableFuture executeVariableBatch(ExecutionInput input) { + int size = input.getVariablesList().size(); + try { + GraphQL executor = prepareVariableBatch(input); + ExecutionId batchId = assertNotNull(ensureInputHasId(input).getExecutionId()); + List> results = new ArrayList<>(size); + for (int index = 0; index < size; index++) { + ExecutionInput entry = input.newVariableBatchEntry(index, batchId); + input.getCancellationFuture().thenRun(entry::cancel); + results.add(executor.executeAsync(entry)); + } + return CompletableFuture.completedFuture(new VariableBatchExecutionResultImpl(results)); + } catch (RuntimeException exception) { + return CompletableFuture.failedFuture(exception); + } + } + + private GraphQL prepareVariableBatch(ExecutionInput input) { + ExecutionInput preparationInput = input.transform(builder -> builder.variables(List.of())); + // Custom providers and instrumentation retain their existing per-execution semantics. + if (preparsedDocumentProvider != NoOpPreparsedDocumentProvider.INSTANCE + || instrumentation.getClass() != SimplePerformantInstrumentation.class) { + assertVariableBatchOperationSupported(true, preparationInput, ParseAndValidate.parse(preparationInput).getDocument()); + return this; + } + PreparsedDocumentEntry prepared = parseAndValidate(new AtomicReference<>(preparationInput), graphQLSchema, null, true); + CompletableFuture preparedFuture = CompletableFuture.completedFuture(prepared); + return transform(builder -> { + builder.preparsedDocumentProvider((entry, compute) -> preparedFuture).valueUnboxer(valueUnboxer); + if (doNotAutomaticallyDispatchDataLoader) { + builder.doNotAutomaticallyDispatchDataLoader(); + } + }); + } + + private void assertVariableBatchOperationSupported(boolean variableBatch, ExecutionInput input, @Nullable Document document) { + if (!variableBatch || document == null) { + return; + } + if (document.getDefinitionsOfType(OperationDefinition.class).isEmpty()) { + return; + } + OperationDefinition operation; + try { + operation = NodeUtil.getOperation(document, input.getOperationName()).operationDefinition; + } catch (UnknownOperationException exception) { + // Keep the normal operation-selection errors when no operation can be selected. + return; + } + if (operation.getOperation() == OperationDefinition.Operation.SUBSCRIPTION) { + throw new IllegalArgumentException("Variable batching does not support subscription operations; supply at most one variable map."); + } + } + private CompletableFuture handleAbortException(ExecutionInput executionInput, InstrumentationState instrumentationState, AbortExecutionException abortException) { InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters(executionInput, this.graphQLSchema); @@ -537,15 +600,16 @@ private ExecutionInput ensureInputHasId(ExecutionInput executionInput) { } - private CompletableFuture parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState, EngineRunningState engineRunningState, Profiler profiler) { + private CompletableFuture parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState, EngineRunningState engineRunningState, Profiler profiler, boolean variableBatch) { AtomicReference executionInputRef = new AtomicReference<>(executionInput); Function computeFunction = transformedInput -> { // if they change the original query in the pre-parser, then we want to see it downstream from then on executionInputRef.set(transformedInput); - return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState); + return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState, variableBatch); }; CompletableFuture preparsedDoc = preparsedDocumentProvider.getDocumentAsync(executionInput, computeFunction); return engineRunningState.compose(preparsedDoc, (preparsedDocumentEntry -> { + assertVariableBatchOperationSupported(variableBatch, assertNotNull(executionInputRef.get()), preparsedDocumentEntry.getDocument()); if (preparsedDocumentEntry.hasErrors()) { return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDocumentEntry.getErrors())); } @@ -557,7 +621,7 @@ private CompletableFuture parseValidateAndExecute(ExecutionInpu })); } - private PreparsedDocumentEntry parseAndValidate(AtomicReference executionInputRef, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { + private PreparsedDocumentEntry parseAndValidate(AtomicReference executionInputRef, GraphQLSchema graphQLSchema, @Nullable InstrumentationState instrumentationState, boolean variableBatch) { ExecutionInput executionInput = assertNotNull(executionInputRef.get()); @@ -566,6 +630,7 @@ private PreparsedDocumentEntry parseAndValidate(AtomicReference return new PreparsedDocumentEntry(assertNotNull(parseResult.getSyntaxException(), "Parse result syntax exception cannot be null when failed").toInvalidSyntaxError()); } else { final Document document = assertNotNull(parseResult.getDocument(), "Document cannot be null when parse succeeded"); + assertVariableBatchOperationSupported(variableBatch, executionInput, document); // they may have changed the document and the variables via instrumentation so update the reference to it executionInput = executionInput.transform(builder -> builder.variables(parseResult.getVariables())); executionInputRef.set(executionInput); @@ -584,7 +649,7 @@ private PreparsedDocumentEntry parseAndValidate(AtomicReference } } - private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { + private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, @Nullable InstrumentationState instrumentationState) { InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema); InstrumentationContext parseInstrumentationCtx = nonNullCtx(instrumentation.beginParse(parameters, instrumentationState)); parseInstrumentationCtx.onDispatched(); @@ -603,7 +668,7 @@ private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchem } } - private List validate(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { + private List validate(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, @Nullable InstrumentationState instrumentationState) { InstrumentationContext> validationCtx = nonNullCtx(instrumentation.beginValidation(new InstrumentationValidationParameters(executionInput, document, graphQLSchema), instrumentationState)); validationCtx.onDispatched(); diff --git a/src/main/java/graphql/VariableBatchExecutionResult.java b/src/main/java/graphql/VariableBatchExecutionResult.java new file mode 100644 index 0000000000..95ba9251ba --- /dev/null +++ b/src/main/java/graphql/VariableBatchExecutionResult.java @@ -0,0 +1,50 @@ +package graphql; + +import org.jspecify.annotations.NullMarked; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * The result of supplying more than one variable map to {@link GraphQL#executeAsync(ExecutionInput)}. + * Each entry is submitted to {@code executeAsync} in input order, without waiting for earlier futures + * to complete. {@link #getResults()} exposes those futures; {@link #getResultsAsync()} collects their + * results in input order. Batching does not create threads or limit the number of executions in flight. + * {@link #getData()} returns the same list of futures as {@link #getResults()}. + * There is no single GraphQL response map for a batch, so {@link #toSpecification()} is unsupported. + * Serialize the individual execution results instead. + *
{@code
+ * ExecutionInput input = ExecutionInput.newExecutionInput()
+ *     .query("query($id: ID!) { product(id: $id) { name } }")
+ *     .variables(List.of(Map.of("id", "a"), Map.of("id", "b")))
+ *     .dataLoaderRegistry(registry)
+ *     .build();
+ * VariableBatchExecutionResult batch = (VariableBatchExecutionResult) graphQL.execute(input);
+ * CompletableFuture> results = batch.getResultsAsync();
+ * }
+ * The entries share DataLoaders and caches for queries and mutations, using ordinary dispatch behavior. + * Mutations retain serial root fields within each entry; there is no ordering guarantee between + * entries. Applications must clear or prime affected loader cache entries after writes. + * Each entry has separate variable coercion, errors, instrumentation state, cancellation state, + * execution id and a shallow copy of the GraphQL context. Application objects in that context, + * the root and the local context remain shared. Cancelling the input requests cancellation of each entry. + * Incremental results retain their individual publishers and lifecycle. + * Subscription operations are rejected with {@link IllegalArgumentException} as soon as the selected operation is known. + */ +@ExperimentalApi +@NullMarked +public interface VariableBatchExecutionResult extends ExecutionResult { + /** @return an immutable list of execution futures in input order */ + List> getResults(); + + /** @return the number of executions in the batch */ + int getSize(); + + /** + * Collects the individual results in input order. This does not start new executions. + * Cancelling the returned future does not cancel the executions; use {@link ExecutionInput#cancel()} instead. + * + * @return a future containing an immutable list of the individual results in input order + */ + CompletableFuture> getResultsAsync(); +} diff --git a/src/main/java/graphql/VariableBatchExecutionResultImpl.java b/src/main/java/graphql/VariableBatchExecutionResultImpl.java new file mode 100644 index 0000000000..33d6346813 --- /dev/null +++ b/src/main/java/graphql/VariableBatchExecutionResultImpl.java @@ -0,0 +1,37 @@ +package graphql; + +import graphql.execution.Async; +import org.jspecify.annotations.NullMarked; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +@Internal +@NullMarked +public final class VariableBatchExecutionResultImpl extends ExecutionResultImpl implements VariableBatchExecutionResult { + public VariableBatchExecutionResultImpl(List> results) { + super(List.copyOf(results), List.of()); + } + + @Override + public List> getResults() { + return Assert.assertNotNull(getData()); + } + + @Override + public int getSize() { + return getResults().size(); + } + + @Override + public CompletableFuture> getResultsAsync() { + return Async., ExecutionResult>each(getResults(), result -> result) + .thenApply(List::copyOf); + } + + @Override + public Map toSpecification() { + throw new UnsupportedOperationException("Serialize each execution result instead of the batch"); + } +} diff --git a/src/main/java/graphql/extensions/ExtensionsBuilder.java b/src/main/java/graphql/extensions/ExtensionsBuilder.java index e65e315e2f..dbc80859f6 100644 --- a/src/main/java/graphql/extensions/ExtensionsBuilder.java +++ b/src/main/java/graphql/extensions/ExtensionsBuilder.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableMap; import graphql.ExecutionResult; +import graphql.Internal; import graphql.PublicApi; import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; @@ -62,6 +63,13 @@ public int getChangeCount() { return changes.size(); } + @Internal + public ExtensionsBuilder copy() { + ExtensionsBuilder copy = new ExtensionsBuilder(extensionsMerger); + copy.changes.addAll(changes); + return copy; + } + /** * Adds new values into the extension builder * diff --git a/src/test/groovy/graphql/ExecutionInputTest.groovy b/src/test/groovy/graphql/ExecutionInputTest.groovy index 7099a495ab..b0c66d4986 100644 --- a/src/test/groovy/graphql/ExecutionInputTest.groovy +++ b/src/test/groovy/graphql/ExecutionInputTest.groovy @@ -49,6 +49,45 @@ class ExecutionInputTest extends Specification { executionInput.toString() != null } + def "single-map variable accessors accept empty and singleton lists"() { + given: + def input = ExecutionInput.newExecutionInput(query).variables(variableMaps).build() + + expect: + input.getRawVariables().toMap() == expected + input.getVariables() == expected + + where: + variableMaps | expected + [] | [:] + [[:]] | [:] + [[id: 1]] | [id: 1] + [[id: null]] | [id: null] + } + + def "single-map variable accessors reject multiple maps"() { + given: + def input = ExecutionInput.newExecutionInput(query).variables(variableMaps).build() + + when: + input.getRawVariables() + + then: + def rawVariablesError = thrown(AssertException) + rawVariablesError.message.contains('requires at most one variable map') + + when: + input.getVariables() + + then: + def variablesError = thrown(AssertException) + variablesError.message.contains('getVariablesList()') + input.getVariablesList() == variableMaps + + where: + variableMaps << [[[:], [:]], [[id: 1], [id: 2]], [[id: 1], [id: 2], [id: 3]]] + } + def "build without locale"() { when: def executionInput = ExecutionInput.newExecutionInput().query(query) diff --git a/src/test/groovy/graphql/GraphQLTest.groovy b/src/test/groovy/graphql/GraphQLTest.groovy index 7b9c48ad7d..8f8f50af52 100644 --- a/src/test/groovy/graphql/GraphQLTest.groovy +++ b/src/test/groovy/graphql/GraphQLTest.groovy @@ -1058,7 +1058,7 @@ many lines'''] given: when: - def input = newExecutionInput().query('query($var:String){ hello(arg: $var) }').variables(null).build() + def input = newExecutionInput().query('query($var:String){ hello(arg: $var) }').variables((Map) null).build() then: def assEx = thrown(AssertException) diff --git a/src/test/groovy/graphql/VariableBatchExecutionTest.groovy b/src/test/groovy/graphql/VariableBatchExecutionTest.groovy new file mode 100644 index 0000000000..4a5c00409f --- /dev/null +++ b/src/test/groovy/graphql/VariableBatchExecutionTest.groovy @@ -0,0 +1,772 @@ +package graphql + +import graphql.execution.ExecutionId +import graphql.execution.instrumentation.SimplePerformantInstrumentation +import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters +import graphql.execution.instrumentation.InstrumentationState +import graphql.execution.instrumentation.DocumentAndVariables +import graphql.execution.preparsed.PreparsedDocumentEntry +import graphql.execution.preparsed.PreparsedDocumentProvider +import graphql.extensions.ExtensionsBuilder +import graphql.incremental.IncrementalExecutionResult +import graphql.parser.Parser +import graphql.parser.ParserOptions +import graphql.parser.ParsingListener +import graphql.schema.DataFetcher +import org.dataloader.BatchLoader +import org.dataloader.DataLoaderFactory +import org.dataloader.DataLoaderRegistry +import reactor.core.publisher.Flux +import spock.lang.Specification +import spock.lang.Timeout + +import java.time.Duration +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger + +import static graphql.ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT +import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING +import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING + +@Timeout(15) +class VariableBatchExecutionTest extends Specification { + def schema = TestUtil.schema('type Query { value(id: Int = 7): Int! }', + [Query: [value: { env -> env.getArgument('id') } as DataFetcher]]) + + def "empty and singleton lists retain ordinary execution"() { + when: + def result = GraphQL.newGraphQL(schema).build().execute(ExecutionInput.newExecutionInput() + .query('query($id:Int) { value(id:$id) }').variables(variables).build()) + + then: + !(result instanceof VariableBatchExecutionResult) + result.toSpecification() == [data: [value: expected]] + + where: + variables | expected + [] | 7 + [[:]] | 7 + [[id: 3]] | 3 + } + + def "legacy variables become a singleton and transforms preserve every map"() { + given: + def maps = [[id: 1], [id: null]] + def input = ExecutionInput.newExecutionInput('{ value }').variables(maps).build() + maps[0].id = 99 + maps.add([id: 3]) + + expect: + ExecutionInput.newExecutionInput('{ value }').variables([id: 1]).build().variablesList == [[id: 1]] + input.variablesList == [[id: 1], [id: null]] + input.transform { it.operationName('Op') }.variablesList == [[id: 1], [id: null]] + + when: + input.variablesList[0].put('id', 4) + + then: + thrown(UnsupportedOperationException) + } + + def "separate variable coercion and conditional selection for every entry"() { + when: + def batch = batch(GraphQL.newGraphQL(schema).build(), + 'query($id: Int!,$include:Boolean! = true) { value(id:$id) @include(if:$include) }', + [[id: 1], [id: 2, include: false], [id: 'bad'], [id: 1]]) + def results = batch.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + results.size() == 4 + results[0].data == [value: 1] + results[1].data == [:] + !results[2].dataPresent + results[2].errors.size() == 1 + results[3].data == [value: 1] + } + + def "common request errors are returned for every index"() { + when: + def results = batch(GraphQL.newGraphQL(schema).build(), query, [[:], [:]]) + .resultsAsync.get(5, TimeUnit.SECONDS) + + then: + results.size() == 2 + results.every { !it.errors.empty && !it.dataPresent } + + where: + query << ['{', '{ missing }', 'query A { value } query B { value }', 'fragment F on Query { value }'] + } + + def "shared dataloaders use ordinary dispatch and cache repeated keys"() { + given: + def calls = [] + def registry = new DataLoaderRegistry() + registry.register('values', DataLoaderFactory.newDataLoader({ keys -> + calls.add(new ArrayList(keys)) + CompletableFuture.completedFuture(keys) + } as BatchLoader)) + def loadedSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> env.getDataLoader('values').load(env.getArgument('id')) } as DataFetcher]]) + def graphQL = GraphQL.newGraphQL(loadedSchema).build() + + when: + def input = ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2], [id: 1]]) + .dataLoaderRegistry(registry).build() + VariableBatchExecutionResult result = graphQL.execute(input) + def entries = result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + entries*.data == [[value: 1], [value: 2], [value: 1]] + calls == [[1], [2]] + } + + def "manual dispatch still applies to the shared registry"() { + given: + def calls = [] + def registry = new DataLoaderRegistry().register('values', DataLoaderFactory.newDataLoader({ keys -> + calls.add(new ArrayList(keys)) + CompletableFuture.completedFuture(keys) + } as BatchLoader)) + def localSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> env.getDataLoader('values').load(env.getArgument('id')) } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2], [id: 1]]).dataLoaderRegistry(registry).build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(localSchema) + .doNotAutomaticallyDispatchDataLoader().build().execute(input) + + then: + result.results.every { !it.done } + calls.empty + + when: + registry.dispatchAll() + + then: + result.resultsAsync.get(5, TimeUnit.SECONDS)*.data == [[value: 1], [value: 2], [value: 1]] + calls == [[1, 2]] + } + + def "asynchronous chained loaders share their registry using the selected dispatch mode"() { + given: + def pending = [(1): new CompletableFuture>(), (2): new CompletableFuture>()] + def firstCalls = [] + def secondCalls = [] + def registry = new DataLoaderRegistry() + registry.register('first', DataLoaderFactory.newDataLoader({ keys -> + firstCalls.add(new ArrayList(keys)) + pending[keys[0]] + } as BatchLoader)) + registry.register('second', DataLoaderFactory.newDataLoader({ keys -> + secondCalls.add(new ArrayList(keys)) + CompletableFuture.completedFuture(keys) + } as BatchLoader)) + def loadedSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> + env.getDataLoader('first').load(env.getArgument('id')).thenCompose { value -> + env.getDataLoader('second').load(value) + } + } as DataFetcher]]) + VariableBatchExecutionResult result = GraphQL.newGraphQL(loadedSchema).build().execute( + ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2], [id: 1]]).dataLoaderRegistry(registry) + .graphQLContext([(dispatchOption): true]).build()) + + expect: + firstCalls == [[1], [2]] + secondCalls.empty + + when: + pending[2].complete([2]) + + then: + result.results[1].get(5, TimeUnit.SECONDS).data == [value: 2] + !result.results[0].done + !result.results[2].done + + when: + pending[1].complete([1]) + + then: + result.resultsAsync.get(5, TimeUnit.SECONDS)*.data == [[value: 1], [value: 2], [value: 1]] + secondCalls == [[2], [1]] + + where: + dispatchOption << [ENABLE_DATA_LOADER_CHAINING, ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING] + } + + def "execution futures can be consumed independently in any completion order"() { + given: + def values = [new CompletableFuture(), new CompletableFuture()] + def asyncSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> values[env.getArgument('id')] } as DataFetcher]]) + def result = batch(GraphQL.newGraphQL(asyncSchema).build(), + 'query($id:Int!) { value(id:$id) }', [[id: 0], [id: 1]]) + + when: + values[1].complete(11) + + then: + result.results[1].get(5, TimeUnit.SECONDS).data == [value: 11] + !result.results[0].done + + when: + values[0].complete(10) + + then: + result.results[0].get(5, TimeUnit.SECONDS).data == [value: 10] + } + + def "non-null failure is confined to its execution"() { + given: + def failingSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> env.getArgument('id') == 1 ? null : 2 } as DataFetcher]]) + + when: + def results = batch(GraphQL.newGraphQL(failingSchema).build(), + 'query($id:Int!) { value(id:$id) }', [[id: 1], [id: 2]]).resultsAsync.get(5, TimeUnit.SECONDS) + + then: + results[0].data == null + results[0].errors[0].path == ['value'] + results[1].toSpecification() == [data: [value: 2]] + } + + def "instrumentation sees individual inputs contexts and ids"() { + given: + def seen = [] + def instrumentation = new SimplePerformantInstrumentation() { + @Override + ExecutionInput instrumentExecutionInput(ExecutionInput input, InstrumentationExecutionParameters parameters, + InstrumentationState state) { + seen.add(input) + input.graphQLContext.put('entry', input.variableIndex) + input.transform { it.variables([id: input.variables.id + 10]) } + } + } + def graphQL = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build() + def input = ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2]]).executionId(ExecutionId.from('batch')) + .graphQLContext([app: 'shared']).build() + + when: + VariableBatchExecutionResult result = graphQL.execute(input) + def results = result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + results*.data == [[value: 11], [value: 12]] + seen*.variableIndex == [0, 1] + seen*.variableBatchId*.toString() == ['batch', 'batch'] + seen*.executionId.toSet().size() == 2 + !seen[0].graphQLContext.is(seen[1].graphQLContext) + !input.graphQLContext.hasKey('entry') + seen.every { it.graphQLContext.get('app') == 'shared' } + } + + def "batch input cancellation reaches every running entry"() { + given: + def invoked = [] + def pending = (1..3).collect { new CompletableFuture() } + def asyncSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> + invoked.add(env.getArgument('id')) + pending[env.getArgument('id') - 1] + } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2], [id: 3]]).build() + VariableBatchExecutionResult batch = GraphQL.newGraphQL(asyncSchema).build().execute(input) + def result = batch.resultsAsync + + when: + input.cancel() + pending.each { it.complete(7) } + + then: + result.get(5, TimeUnit.SECONDS).every { !it.errors.empty } + invoked == [1, 2, 3] + } + + def "subscription batches throw before producing a batch result"() { + given: + def invoked = false + def subscriptionSchema = TestUtil.schema('type Query { value:Int } type Subscription { value:Int }', + [Subscription: [value: { invoked = true } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput(query).operationName(operationName).variables([[:], [:]]).build() + + when: + GraphQL.newGraphQL(subscriptionSchema).build().execute(input) + + then: + def error = thrown(IllegalArgumentException) + error.message == 'Variable batching does not support subscription operations; supply at most one variable map.' + !invoked + + where: + query | operationName + 'subscription { value }' | null + '# comment\nsubscription S { value }' | 'S' + 'subscription { missing }' | null + 'query Q { value } subscription S { value }' | 'S' + } + + def "async subscription batches fail before instrumentation or variable coercion"() { + given: + def instrumentation = Mock(graphql.execution.instrumentation.Instrumentation) + def input = ExecutionInput.newExecutionInput('subscription($id:Int!) { value(id:$id) }') + .variables([[id: 'bad'], [:]]).build() + + when: + def result = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build().executeAsync(input) + result.get(5, TimeUnit.SECONDS) + + then: + def error = thrown(java.util.concurrent.ExecutionException) + error.cause instanceof IllegalArgumentException + error.cause.message.contains('subscription operations') + 0 * instrumentation._ + } + + def "empty and singleton variable lists still allow ordinary subscriptions"() { + given: + def subscriptionSchema = TestUtil.schema('type Query { value:Int } type Subscription { value:Int }', + [Subscription: [value: { Flux.just(7) } as DataFetcher]]) + + when: + def result = GraphQL.newGraphQL(subscriptionSchema).build().execute( + ExecutionInput.newExecutionInput('subscription { value }').variables(variables).build()) + def events = Flux.from(result.data).collectList().block(Duration.ofSeconds(5)) + + then: + !(result instanceof VariableBatchExecutionResult) + events*.data == [[value: 7]] + + where: + variables << [[], [[:]]] + } + + def "a query can be selected from a document that also contains a subscription"() { + given: + def subscriptionSchema = TestUtil.schema('type Query { value:Int } type Subscription { value:Int }', + [Query: [value: { 7 } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('query Q { value } subscription S { value }') + .operationName('Q').variables([[:], [:]]).build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(subscriptionSchema).build().execute(input) + + then: + result.resultsAsync.get(5, TimeUnit.SECONDS)*.toSpecification() == [[data: [value: 7]], [data: [value: 7]]] + } + + def "cached persisted subscriptions fail the whole batch before any resolver runs"() { + given: + def invoked = false + def subscriptionSchema = TestUtil.schema('type Query { value:Int } type Subscription { value:Int }', + [Subscription: [value: { invoked = true; Flux.just(7) } as DataFetcher]]) + def provider = { input, compute -> CompletableFuture.completedFuture( + new PreparsedDocumentEntry(Parser.parse('subscription { value }'))) } as PreparsedDocumentProvider + def input = ExecutionInput.newExecutionInput(ExecutionInput.PERSISTED_QUERY_MARKER).variables([[:], [:]]).build() + VariableBatchExecutionResult result = GraphQL.newGraphQL(subscriptionSchema).preparsedDocumentProvider(provider).build().execute(input) + + when: + result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + def error = thrown(java.util.concurrent.ExecutionException) + error.cause instanceof IllegalArgumentException + error.cause.message.contains('subscription operations') + !invoked + } + + def "document instrumentation cannot introduce a batched subscription"() { + given: + def instrumentation = new SimplePerformantInstrumentation() { + @Override + DocumentAndVariables instrumentDocumentAndVariables(DocumentAndVariables value, + InstrumentationExecutionParameters parameters, InstrumentationState state) { + value.transform { it.document(Parser.parse('subscription { missing }')) } + } + } + + when: + batch(GraphQL.newGraphQL(schema).instrumentation(instrumentation).build(), '{ value }', [[:], [:]]) + .resultsAsync.get(5, TimeUnit.SECONDS) + + then: + def error = thrown(java.util.concurrent.ExecutionException) + error.cause instanceof IllegalArgumentException + error.cause.message.contains('subscription operations') + } + + def "replacing an execution input cannot bypass the subscription batch restriction"() { + given: + def instrumentation = new SimplePerformantInstrumentation() { + @Override + ExecutionInput instrumentExecutionInput(ExecutionInput input, InstrumentationExecutionParameters parameters, + InstrumentationState state) { + ExecutionInput.newExecutionInput('subscription { value }').build() + } + } + + when: + batch(GraphQL.newGraphQL(schema).instrumentation(instrumentation).build(), '{ value }', [[:], [:]]) + .resultsAsync.get(5, TimeUnit.SECONDS) + + then: + def error = thrown(java.util.concurrent.ExecutionException) + error.cause instanceof IllegalArgumentException + error.cause.message.contains('subscription operations') + } + + def "collected results keep input order across asynchronous completion"() { + given: + def values = [new CompletableFuture(), new CompletableFuture()] + def asyncSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> values[env.getArgument('id')] } as DataFetcher]]) + def result = batch(GraphQL.newGraphQL(asyncSchema).build(), 'query($id:Int!) { value(id:$id) }', + [[id: 0], [id: 1]]).resultsAsync + + when: + values[1].complete(11) + values[0].complete(10) + + then: + result.get(5, TimeUnit.SECONDS)*.data == [[value: 10], [value: 11]] + } + + def "mutation fields stay serial and cache invalidation applies to the shared registry"() { + given: + def values = [key: 0] + def calls = [] + def registry = new DataLoaderRegistry() + def loader = DataLoaderFactory.newDataLoader({ keys -> + calls.add(new ArrayList(keys)) + CompletableFuture.completedFuture(keys.collect { values[it] }) + } as BatchLoader) + registry.register('values', loader) + def mutationSchema = TestUtil.schema(''' + type Query { value:Int } + type Mutation { update(value:Int!):Int! read:Int! } + ''', [Mutation: [update: { env -> + values.key = env.getArgument('value') + env.getDataLoader('values').clear('key').prime('key', values.key) + values.key + } as DataFetcher, read: { env -> env.getDataLoader('values').load('key') } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('mutation($value:Int!) { update(value:$value) read }') + .variables([[value: 1], [value: 2]]).dataLoaderRegistry(registry).build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(mutationSchema) + .build().execute(input) + def entries = result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + entries*.data == [[update: 1, read: 1], [update: 2, read: 2]] + calls.empty + values.key == 2 + } + + def "extension builders are isolated including a supplied builder"() { + given: + def extensions = ExtensionsBuilder.newExtensionsBuilder().addValue('initial', true) + def localSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> + env.graphQlContext.get(ExtensionsBuilder).addValue('entry', env.getArgument('id')) + env.getArgument('id') + } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2]]).graphQLContext([(ExtensionsBuilder): extensions]).context('legacy').build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(localSchema).build().execute(input) + def entries = result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + entries*.extensions == [[initial: true, entry: 1], [initial: true, entry: 2]] + extensions.buildExtensions() == [initial: true] + } + + def "custom document instrumentation and preparsed providers retain per-entry behavior"() { + given: + def seen = [] + def provider = { ExecutionInput input, compute -> + seen.add(input.variables.id) + CompletableFuture.completedFuture(compute.apply(input)) + } as PreparsedDocumentProvider + def instrumentation = new SimplePerformantInstrumentation() { + @Override + DocumentAndVariables instrumentDocumentAndVariables(DocumentAndVariables value, + InstrumentationExecutionParameters parameters, InstrumentationState state) { + value.transform { it.variables([id: parameters.variables.id + 10]) } + } + } + + when: + def result = batch(GraphQL.newGraphQL(schema).preparsedDocumentProvider(provider) + .instrumentation(instrumentation).build(), 'query($id:Int!) { value(id:$id) }', [[id: 1], [id: 2]]) + def entries = result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + seen == [1, 2] + entries*.data == [[value: 11], [value: 12]] + } + + def "default batch preparation runs eagerly and is reused by every entry"() { + given: + def calls = new AtomicInteger() + def listener = { token -> if (token.text == 'query') calls.incrementAndGet() } as ParsingListener + def options = ParserOptions.newParserOptions().parsingListener(listener).build() + def input = ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2]]).graphQLContext([(ParserOptions): options]).build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(schema).build().execute(input) + + then: + calls.get() == 1 + + when: + def entries = result.resultsAsync.get(5, TimeUnit.SECONDS) + + then: + entries*.data == [[value: 1], [value: 2]] + calls.get() == 1 + } + + def "incremental execution remains available per variable entry"() { + given: + def seen = [] + def instrumentation = new SimplePerformantInstrumentation() { + @Override + CompletableFuture instrumentExecutionResult(ExecutionResult result, + InstrumentationExecutionParameters parameters, InstrumentationState state) { + seen.add(result) + CompletableFuture.completedFuture(result) + } + } + def registry = new DataLoaderRegistry() + registry.register('values', DataLoaderFactory.newDataLoader({ keys -> CompletableFuture.completedFuture(keys) } as BatchLoader)) + def localSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> env.getDataLoader('values').load(env.getArgument('id')) } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('query($id:Int!) { ... @defer { value(id:$id) } }') + .variables([[id: 1], [id: 2]]).dataLoaderRegistry(registry) + .graphQLContext([(ENABLE_INCREMENTAL_SUPPORT): true]).build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(localSchema).instrumentation(instrumentation).build().execute(input) + def entries = result.resultsAsync.get(5, TimeUnit.SECONDS) + def payloads = entries.collect { IncrementalExecutionResult entry -> + Flux.from(entry.incrementalItemPublisher).collectList().block(Duration.ofSeconds(5)) + } + + then: + entries.every { it instanceof IncrementalExecutionResult } + entries[0].is(seen[0]) + entries[1].is(seen[1]) + payloads.collect { it[0].incremental[0].data } == [[value: 1], [value: 2]] + } + + def "an asynchronous shared loader failure belongs to each affected entry"() { + given: + def pending = new CompletableFuture>() + def keysSeen = [] + def registry = new DataLoaderRegistry().register('values', DataLoaderFactory.newDataLoader({ keys -> + keysSeen.add(new ArrayList(keys)) + pending + } as BatchLoader)) + def localSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> env.getDataLoader('values').load(env.getArgument('id')) } as DataFetcher]]) + VariableBatchExecutionResult result = GraphQL.newGraphQL(localSchema).build().execute( + ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 2]]).dataLoaderRegistry(registry).build()) + def entries = result.resultsAsync + + when: + pending.completeExceptionally(new IllegalStateException('backend unavailable')) + + then: + entries.get(5, TimeUnit.SECONDS).every { it.data == null && it.errors[0].path == ['value'] } + keysSeen == [[1], [2]] + } + + def "cancelling an individual execution does not cancel a shared loader future"() { + given: + def seen = [] + def instrumentation = new SimplePerformantInstrumentation() { + @Override + ExecutionInput instrumentExecutionInput(ExecutionInput input, InstrumentationExecutionParameters parameters, + InstrumentationState state) { + seen.add(input) + input + } + } + def pending = new CompletableFuture>() + def registry = new DataLoaderRegistry().register('values', DataLoaderFactory.newDataLoader({ keys -> pending } as BatchLoader)) + def localSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> env.getDataLoader('values').load(env.getArgument('id')) } as DataFetcher]]) + VariableBatchExecutionResult result = GraphQL.newGraphQL(localSchema).instrumentation(instrumentation).build().execute( + ExecutionInput.newExecutionInput('query($id:Int!) { value(id:$id) }') + .variables([[id: 1], [id: 1]]).dataLoaderRegistry(registry).build()) + def entries = result.resultsAsync + + when: + seen[0].cancel() + pending.complete([10]) + + then: + !pending.cancelled + !entries.get(5, TimeUnit.SECONDS)[0].errors.empty + entries.get(5, TimeUnit.SECONDS)[1].toSpecification() == [data: [value: 10]] + } + + def "pre-cancelled input cancels every entry before fetching"() { + given: + def calls = [] + def localSchema = TestUtil.schema('type Query { value:Int! }', + [Query: [value: { calls.add(1); 7 } as DataFetcher]]) + def input = ExecutionInput.newExecutionInput('{ value }').variables([[:], [:]]).build() + input.cancel() + + when: + VariableBatchExecutionResult batch = GraphQL.newGraphQL(localSchema).build().execute(input) + + then: + batch.resultsAsync.get(5, TimeUnit.SECONDS).every { !it.errors.empty } + calls.empty + } + + def "cancelling result collection leaves the independent executions running"() { + given: + def values = [new CompletableFuture(), new CompletableFuture()] + def asyncSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> values[env.getArgument('id')] } as DataFetcher]]) + def result = batch(GraphQL.newGraphQL(asyncSchema).build(), + 'query($id:Int!) { value(id:$id) }', [[id: 0], [id: 1]]) + def collected = result.resultsAsync + + when: + collected.cancel(false) + values[1].complete(11) + values[0].complete(10) + + then: + collected.cancelled + result.resultsAsync.get(5, TimeUnit.SECONDS)*.data == [[value: 10], [value: 11]] + } + + def "batch exposes immutable futures and supports repeated result collection"() { + given: + def result = batch(GraphQL.newGraphQL(schema).build(), '{ value }', [[:], [:]]) + def collected = result.resultsAsync.get(5, TimeUnit.SECONDS) + + expect: + result.data.is(result.results) + result.size == 2 + result.resultsAsync.get(5, TimeUnit.SECONDS) == collected + + when: + result.results.clear() + + then: + thrown(UnsupportedOperationException) + + when: + collected.clear() + + then: + thrown(UnsupportedOperationException) + + when: + result.toSpecification() + + then: + thrown(UnsupportedOperationException) + } + + def "an exceptional execution does not prevent the other entries from starting"() { + given: + def seen = [] + def instrumentation = new SimplePerformantInstrumentation() { + @Override + ExecutionInput instrumentExecutionInput(ExecutionInput input, InstrumentationExecutionParameters parameters, + InstrumentationState state) { + seen.add(input.variableIndex) + if (input.variableIndex == 0) throw new IllegalStateException('broken instrumentation') + input + } + } + + when: + batch(GraphQL.newGraphQL(schema).instrumentation(instrumentation).build(), + '{ value }', [[:], [:]]).resultsAsync.get(5, TimeUnit.SECONDS) + + then: + def failure = thrown(java.util.concurrent.ExecutionException) + failure.cause.message == 'broken instrumentation' + seen == [0, 1] + } + + def "all entries start immediately without batch size or concurrency limits"() { + given: + def called = [] + def pending = (0..1024).collect { new CompletableFuture() } + def localSchema = TestUtil.schema('type Query { value(id:Int!):Int! }', + [Query: [value: { env -> + def id = env.getArgument('id') + called.add(id) + pending[id] + } as DataFetcher]]) + + when: + def result = batch(GraphQL.newGraphQL(localSchema).build(), + 'query($id:Int!) { value(id:$id) }', (0..1024).collect { [id: it] }) + + then: + called == (0..1024).toList() + result.results.every { !it.done } + + when: + (1024..0).each { pending[it].complete(it) } + + then: + result.resultsAsync.get(5, TimeUnit.SECONDS)*.data == (0..1024).collect { [value: it] } + } + + def "persisted query lookup can replace the query while retaining every variables map"() { + given: + def seen = [] + def provider = { ExecutionInput input, compute -> + seen.add(input.extensions.id) + CompletableFuture.completedFuture(compute.apply(input.transform { + it.query('query($id:Int!) { value(id:$id) }') + })) + } as PreparsedDocumentProvider + def input = ExecutionInput.newExecutionInput(graphql.execution.preparsed.persisted.PersistedQuerySupport.PERSISTED_QUERY_MARKER) + .extensions([id: 'known-query']).variables([[id: 1], [id: 2]]).build() + + when: + VariableBatchExecutionResult result = GraphQL.newGraphQL(schema).preparsedDocumentProvider(provider).build().execute(input) + + then: + result.resultsAsync.get(5, TimeUnit.SECONDS)*.data == [[value: 1], [value: 2]] + seen == ['known-query', 'known-query'] + } + + def "variable lists reject a null list or a null entry"() { + when: + ExecutionInput.newExecutionInput('{ value }').variables((List>) values) + + then: + thrown(AssertException) + + where: + values << [null, [[id: 1], null]] + } + + private static VariableBatchExecutionResult batch(GraphQL graphQL, String query, List> variables) { + graphQL.execute(ExecutionInput.newExecutionInput(query).variables(variables).build()) as VariableBatchExecutionResult + } +}