8000 This removes the CompletableFuture signature from InstrumentationContext by bbakerman · Pull Request #3457 · graphql-java/graphql-java · GitHub
[go: up one dir, main page]

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
13 changes: 4 additions & 9 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,16 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters(executionInputWithId, this.graphQLSchema, instrumentationState);
ExecutionInput instrumentedExecutionInput = instrumentation.instrumentExecutionInput(executionInputWithId, inputInstrumentationParameters, instrumentationState);

CompletableFuture<ExecutionResult> beginExecutionCF = new CompletableFuture<>();
InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters(instrumentedExecutionInput, this.graphQLSchema, instrumentationState);
InstrumentationContext<ExecutionResult> executionInstrumentation = nonNullCtx(instrumentation.beginExecution(instrumentationParameters, instrumentationState));
executionInstrumentation.onDispatched(beginExecutionCF);
executionInstrumentation.onDispatched();

GraphQLSchema graphQLSchema = instrumentation.instrumentSchema(this.graphQLSchema, instrumentationParameters, instrumentationState);

CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(instrumentedExecutionInput, graphQLSchema, instrumentationState);
//
// finish up instrumentation
executionResult = executionResult.whenComplete(completeInstrumentationCtxCF(executionInstrumentation, beginExecutionCF));
executionResult = executionResult.whenComplete(completeInstrumentationCtxCF(executionInstrumentation));
//
// allow instrumentation to tweak the result
executionResult = executionResult.thenCompose(result -> instrumentation.instrumentExecutionResult(result, instrumentationParameters, instrumentationState));
Expand Down Expand Up @@ -507,15 +506,13 @@ private PreparsedDocumentEntry parseAndValidate(AtomicReference<ExecutionInput>
private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState);
InstrumentationContext<Document> parseInstrumentationCtx = nonNullCtx(instrumentation.beginParse(parameters, instrumentationState));
CompletableFuture<Document> documentCF = new CompletableFuture<>();
parseInstrumentationCtx.onDispatched(documentCF);
parseInstrumentationCtx.onDispatched();

ParseAndValidateResult parseResult = ParseAndValidate.parse(executionInput);
if (parseResult.isFailure()) {
parseInstrumentationCtx.onCompleted(null, parseResult.getSyntaxException());
return parseResult;
} else {
documentCF.complete(parseResult.getDocument());
parseInstrumentationCtx.onCompleted(parseResult.getDocument(), null);

DocumentAndVariables documentAndVariables = parseResult.getDocumentAndVariables();
Expand All @@ -527,15 +524,13 @@ private ParseAndValidateResult parse(ExecutionInput executionInput, GraphQLSchem

private List<ValidationError> validate(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
InstrumentationContext<List<ValidationError>> validationCtx = nonNullCtx(instrumentation.beginValidation(new InstrumentationValidationParameters(executionInput, document, graphQLSchema, instrumentationState), instrumentationState));
CompletableFuture<List<ValidationError>> cf = new CompletableFuture<>();
validationCtx.onDispatched(cf);
validationCtx.onDispatched();

Predicate<Class<?>> validationRulePredicate = executionInput.getGraphQLContext().getOrDefault(ParseAndValidate.INTERNAL_VALIDATION_PREDICATE_HINT, r -> true);
Locale locale = executionInput.getLocale() != null ? executionInput.getLocale() : Locale.getDefault();
List<ValidationError> validationErrors = ParseAndValidate.validate(graphQLSchema, document, validationRulePredicate, locale);

validationCtx.onCompleted(validationErrors, null);
cf.complete(validationErrors);
return validationErrors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
Async.CombinedBuilder<FieldValueInfo> futures = getAsyncFieldValueInfo(executionContext, parameters, deferredExecutionSupport);

CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
executionStrategyCtx.onDispatched(overallResult);
executionStrategyCtx.onDispatched();

futures.await().whenComplete((completeValueInfos, throwable) -> {
List<String> fieldsExecutedOnInitialResult = deferredExecutionSupport.getNonDeferredFieldNames(fieldNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
});

CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>();
executionStrategyCtx.onDispatched(overallResult);
executionStrategyCtx.onDispatched();

resultsFuture.whenComplete(handleResults(executionContext, fieldNames, overallResult));
overallResult.whenComplete(executionStrategyCtx::onCompleted);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
ExecutionResult executionResult = new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte));
CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(executionResult);

executeOperationCtx.onDispatched(resultCompletableFuture);
executeOperationCtx.onDispatched();
executeOperationCtx.onCompleted(executionResult, rte);
return resultCompletableFuture;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
}

// note this happens NOW - not when the result completes
executeOperationCtx.onDispatched(result);
executeOperationCtx.onDispatched();

// fill out extensions if we have them
result = result.thenApply(er -> mergeExtensionsBuilderIfPresent(er, graphQLContext));
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected CompletableFuture<Map<String, Object>> executeObject(ExecutionContext
Async.CombinedBuilder<FieldValueInfo> resolvedFieldFutures = getAsyncFieldValueInfo(executionContext, parameters, deferredExecutionSupport);

CompletableFuture<Map<String, Object>> overallResult = new CompletableFuture<>();
resolveObjectCtx.onDispatched(overallResult);
resolveObjectCtx.onDispatched();

resolvedFieldFutures.await().whenComplete((completeValueInfos, throwable) -> {
List<String> fieldsExecutedOnInitialResult = deferredExecutionSupport.getNonDeferredFieldNames(fieldNames);
Expand Down Expand Up @@ -354,7 +354,7 @@ protected CompletableFuture<FieldValueInfo> resolveFieldWithInfo(ExecutionContex

CompletableFuture<Object> fieldValueFuture = result.thenCompose(FieldValueInfo::getFieldValueFuture);

fieldCtx.onDispatched(fieldValueFuture);
fieldCtx.onDispatched();
fieldValueFuture.whenComplete(fieldCtx::onCompleted);
return result;
}
Expand Down Expand Up @@ -425,7 +425,7 @@ protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionC
dataFetcher = executionContext.getDataLoaderDispatcherStrategy().modifyDataFetcher(dataFetcher);
CompletableFuture<Object> fetchedValue = invokeDataFetcher(executionContext, parameters, fieldDef, dataFetchingEnvironment, dataFetcher);
executionContext.getDataLoaderDispatcherStrategy().fieldFetched(executionContext, parameters, dataFetcher, fetchedValue);
fetchCtx.onDispatched(fetchedValue);
fetchCtx.onDispatched();
return fetchedValue
.handle((result, exception) -> {
fetchCtx.onCompleted(result, exception);
Expand Down Expand Up @@ -575,7 +575,7 @@ protected FieldValueInfo completeField(ExecutionContext e BB7C xecutionContext, Execut
FieldValueInfo fieldValueInfo = completeValue(executionContext, newParameters);

CompletableFuture<Object> executionResultFuture = fieldValueInfo.getFieldValueFuture();
ctxCompleteField.onDispatched(executionResultFuture);
ctxCompleteField.onDispatched();
executionResultFuture.whenComplete(ctxCompleteField::onCompleted);
return fieldValueInfo;
}
Expand Down Expand Up @@ -729,7 +729,7 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
CompletableFuture<List<Object>> resultsFuture = Async.each(fieldValueInfos, FieldValueInfo::getFieldValueFuture);

CompletableFuture<Object> overallResult = new CompletableFuture<>();
completeListCtx.onDispatched(overallResult);
completeListCtx.onDispatched();
overallResult.whenComplete(completeListCtx::onCompleted);

resultsFuture.whenComplete((results, exception) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
});

// dispatched the subscription query
executionStrategyCtx.onDispatched(overallResult);
executionStrategyCtx.onDispatched();
overallResult.whenComplete(executionStrategyCtx::onCompleted);

return overallResult;
Expand Down Expand Up @@ -140,7 +140,7 @@ private CompletableFuture<ExecutionResult> executeSubscriptionEvent(ExecutionCon
.thenApply(executionResult -> wrapWithRootFieldName(newParameters, executionResult));

// dispatch instrumentation so they can know about each subscription event
subscribedFieldCtx.onDispatched(overallResult);
subscribedFieldCtx.onDispatched();
overallResult.whenComplete(subscribedFieldCtx::onCompleted);

// allow them to instrument each ER should they want to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ private static class ChainedInstrumentationContext<T> implements Instrumentation
}

@Override
public void onDispatched(CompletableFuture<T> result) {
contexts.forEach(context -> context.onDispatched(result));
public void onDispatched() {
contexts.forEach(InstrumentationContext::onDispatched);
}

@Override
Expand All @@ -416,8 +416,8 @@ private static class ChainedExecutionStrategyInstrumentationContext implements E
}

@Override
public void onDispatched(CompletableFuture<ExecutionResult> result) {
contexts.forEach(context -> context.onDispatched(result));
public void onDispatched() {
contexts.forEach(InstrumentationContext::onDispatched);
}

@Override
Expand Down Expand Up @@ -445,8 +445,8 @@ private static class ChainedExecuteObjectInstrumentationContext implements Execu
}

@Override
public void onDispatched(CompletableFuture<Map<String, Object>> result) {
contexts.forEach(context -> context.onDispatched(result));
public void onDispatched() {
contexts.forEach(InstrumentationContext::onDispatched);
}

@Override
Expand Down Expand Up @@ -474,8 +474,8 @@ private static class ChainedDeferredExecutionStrategyInstrumentationContext impl
}

@Override
public void onDispatched(CompletableFuture<Object> result) {
contexts.forEach(context -> context.onDispatched(result));
public void onDispatched() {
contexts.forEach(InstrumentationContext::onDispatched);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ExecuteObjectInstrumentationContext extends InstrumentationCont
@Internal
ExecuteObjectInstrumentationContext NOOP = new ExecuteObjectInstrumentationContext() {
@Override
public void onDispatched(CompletableFuture<Map<String, Object>> result) {
public void onDispatched() {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static ExecutionStrategyInstrumentationContext nonNullCtx(ExecutionStrategyInstr
@Internal
ExecutionStrategyInstrumentationContext NOOP = new ExecutionStrategyInstrumentationContext() {
@Override
public void onDispatched(CompletableFuture<ExecutionResult> result) {
public void onDispatched() {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public interface InstrumentationContext<T> {

/**
* This is invoked when the instrumentation step is initially dispatched
*
* @param result the result of the step as a completable future
*/
void onDispatched(CompletableFuture<T> result);
void onDispatched();

/**
* This is invoked when the instrumentation step is fully completed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SimpleInstrumentationContext<T> implements InstrumentationContext<T

private static final InstrumentationContext<Object> NO_OP = new InstrumentationContext<Object>() {
@Override
public void onDispatched(CompletableFuture<Object> result) {
public void onDispatched() {
}

@Override
Expand Down Expand Up @@ -49,21 +49,21 @@ public static <T> InstrumentationContext<T> nonNullCtx(InstrumentationContext<T>
}

private final BiConsumer<T, Throwable> codeToRunOnComplete;
private final Consumer<CompletableFuture<T>> codeToRunOnDispatch;
private final Runnable codeToRunOnDispatch;

public SimpleInstrumentationContext() {
this(null, null);
}

private SimpleInstrumentationContext(Consumer<CompletableFuture<T>> codeToRunOnDispatch, BiConsumer<T, Throwable> codeToRunOnComplete) {
private SimpleInstrumentationContext(Runnable codeToRunOnDispatch, BiConsumer<T, Throwable> codeToRunOnComplete) {
this.codeToRunOnComplete = codeToRunOnComplete;
this.codeToRunOnDispatch = codeToRunOnDispatch;
}

@Override
public void onDispatched(CompletableFuture<T> result) {
public void onDispatched() {
if (codeToRunOnDispatch != null) {
codeToRunOnDispatch.accept(result);
codeToRunOnDispatch.run();
}
}

Expand All @@ -83,7 +83,7 @@ public void onCompleted(T result, Throwable t) {
*
* @return an instrumentation context
*/
public static <U> SimpleInstrumentationContext<U> whenDispatched(Consumer<CompletableFuture<U>> codeToRun) {
public static <U> SimpleInstrumentationContext<U> whenDispatched(Runnable codeToRun) {
return new SimpleInstrumentationContext<>(codeToRun, null);
}

Expand All @@ -101,13 +101,8 @@ public static <U> SimpleInstrumentationContext<U> whenCompleted(BiConsumer<U, Th
}

public static <T> BiConsumer<? super T, ? super Throwable> completeInstrumentationCtxCF(
InstrumentationContext<T> instrumentationContext, CompletableFuture<T> targetCF) {
InstrumentationContext<T> instrumentationContext) {
return (result, throwable) -> {
if (throwable != null) {
targetCF.completeExceptionally(throwable);
} else {
targetCF.complete(result);
}
nonNullCtx(instrumentationContext).onCompleted(result, throwable);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import graphql.ExecutionResultImpl;
import graphql.Internal;
import graphql.execution.FieldValueInfo;
import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext;
import graphql.execution.instrumentation.ExecuteObjectInstrumentationContext;
import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
* A class to help adapt old {@link ExecutionResult} based ExecutionStrategyInstrumentationContext
Expand All @@ -25,16 +24,17 @@ public ExecuteObjectInstrumentationContextAdapter(ExecutionStrategyInstrumentati
}

@Override
public void onDispatched(CompletableFuture<Map<String, Object>> result) {
CompletableFuture<ExecutionResult> future = result.thenApply(r -> ExecutionResultImpl.newExecutionResult().data(r).build());
delegate.onDispatched(future);
//
// when the mapped future is completed, then call onCompleted on the delegate
future.whenComplete(delegate::onCompleted);
public void onDispatched() {
delegate.onDispatched();
}

@Override
public void onCompleted(Map<String, Object> result, Throwable t) {
if (t != null) {
delegate.onCompleted(null, t);
} else {
delegate.onCompleted(ExecutionResultImpl.newExecutionResult().data(result).build(), null);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import graphql.Internal;
import graphql.execution.instrumentation.InstrumentationContext;

import java.util.concurrent.CompletableFuture;

/**
* A class to help adapt old {@link ExecutionResult} based InstrumentationContext
* from the newer {@link Object} based ones.
Expand All @@ -21,15 +19,16 @@ public ExecutionResultInstrumentationContextAdapter(InstrumentationContext<Execu
}

@Override
public void onDispatched(CompletableFuture<Object> result) {
CompletableFuture<ExecutionResult> future = result.thenApply(obj -> ExecutionResultImpl.newExecutionResult().data(obj).build());
delegate.onDispatched(future);
//
// when the mapped future is completed, then call onCompleted on the delegate
future.whenComplete(delegate::onCompleted);
public void onDispatched() {
delegate.onDispatched();
}

@Override
public void onCompleted(Object result, Throwable t) {
if (t != null) {
delegate.onCompleted(null, t);
} else {
delegate.onCompleted(ExecutionResultImpl.newExecutionResult().data(result).build(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ abstract class AsyncExecutionStrategyTest extends Specification {
}

@Override
void onDispatched(CompletableFuture<ExecutionResult> result) {
void onDispatched() {
}

@Override
Expand Down
BAA3
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class InstrumentationTest extends Specification {
return new ExecutionStrategyInstrumentationContext() {

@Override
void onDispatched(CompletableFuture<ExecutionResult> result) {
void onDispatched() {
System.out.println(String.format("t%s setting go signal on", Thread.currentThread().getId()))
goSignal.set(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestingInstrumentContext<T> implements InstrumentationContext<T> {
}

@Override
void onDispatched(CompletableFuture<T> result) {
void onDispatched() {
if (useOnDispatch) {
this.executionList << "onDispatched:$op"
}
Expand Down
0