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
9 changes: 6 additions & 3 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import static graphql.Assert.assertNotNull;
import static graphql.execution.ExecutionIdProvider.DEFAULT_EXECUTION_ID_PROVIDER;
import static graphql.execution.instrumentation.SimpleInstrumentationContext.completeInstrumentationCtxCF;

/**
* This class is where all graphql-java query execution begins. It combines the objects that are needed
Expand Down Expand Up @@ -513,15 +514,17 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters(executionInput, this.graphQLSchema, instrumentationState);
executionInput = instrumentation.instrumentExecutionInput(executionInput, inputInstrumentationParameters);

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

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

CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(executionInput, graphQLSchema, instrumentationState);
//
// finish up instrumentation
executionResult = executionResult.whenComplete(executionInstrumentation::onCompleted);
executionResult = executionResult.whenComplete(completeInstrumentationCtxCF(executionInstrumentation, beginExecutionCF));
//
// allow instrumentation to tweak the result
executionResult = executionResult.thenCompose(result -> instrumentation.instrumentExecutionResult(result, instrumentationParameters));
Expand Down Expand Up @@ -556,7 +559,7 @@ private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInpu
}
try {
return execute(executionInputRef.get(), preparsedDocumentEntry.getDocument(), graphQLSchema, instrumentationState);
} catch (AbortExecutionException e){
} catch (AbortExecutionException e) {
return CompletableFuture.completedFuture(e.toExecutionResult());
}
});
Expand Down Expand Up @@ -680,4 +683,4 @@ private static Instrumentation checkInstrumentationDefaultState(Instrumentation
}
return new ChainedInstrumentation(instrumentationList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,17 @@ public static <U> SimpleInstrumentationContext<U> whenDispatched(Consumer<Comple
public static <U> SimpleInstrumentationContext<U> whenCompleted(BiConsumer<U, Throwable> codeToRun) {
return new SimpleInstrumentationContext<>(null, codeToRun);
}

public static <T> BiConsumer<? super T, ? super Throwable> completeInstrumentationCtxCF(
InstrumentationContext<T> instrumentationContext, CompletableFuture<T> targetCF) {
return (result, throwable) -> {
if (throwable != null) {
targetCF.completeExceptionally(throwable);
} else {
targetCF.complete(result);
}
instrumentationContext.onCompleted(result, throwable);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,46 @@ class InstrumentationTest extends Specification {

def expected = [
"start:execution",

"onDispatched:execution",
"start:parse",
"onDispatched:parse",
"end:parse",

"start:validation",
"onDispatched:validation",
"end:validation",

"start:execute-operation",

"start:execution-strategy",

"start:field-hero",
"start:fetch-hero",
"onDispatched:fetch-hero",
"end:fetch-hero",

"start:complete-hero",

"start:execution-strategy",

"start:field-id",
"start:fetch-id",
"onDispatched:fetch-id",
"end:fetch-id",
"start:complete-id",
"onDispatched:complete-id",
"end:complete-id",
"onDispatched:field-id",
"end:field-id",

"onDispatched:execution-strategy",
"end:execution-strategy",

"onDispatched:complete-hero",
"end:complete-hero",
"onDispatched:field-hero",
"end:field-hero",

"onDispatched:execution-strategy",
"end:execution-strategy",

"onDispatched:execute-operation",
"end:execute-operation",
"end:execution",
]
when:

def instrumentation = new TestingInstrumentation()
instrumentation.useOnDispatch = true

def graphQL = GraphQL
.newGraphQL(StarWarsSchema.starWarsSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import graphql.ExecutionResult

class TestingExecutionStrategyInstrumentationContext extends TestingInstrumentContext<ExecutionResult> implements ExecutionStrategyInstrumentationContext {

TestingExecutionStrategyInstrumentationContext(Object op, Object executionList, Object throwableList) {
super(op, executionList, throwableList)
TestingExecutionStrategyInstrumentationContext(Object op, Object executionList, Object throwableList, Boolean useOnDispatch) {
super(op, executionList, throwableList,useOnDispatch)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ class TestingInstrumentContext<T> implements InstrumentationContext<T> {
def start = System.currentTimeMillis()
def executionList = []
def throwableList = []
def useOnDispatch

TestingInstrumentContext(op, executionList, throwableList) {
TestingInstrumentContext(op, executionList, throwableList, useOnDispatch) {
this.op = op
this.executionList = executionList
this.throwableList = throwableList
this.useOnDispatch = useOnDispatch
executionList << "start:$op"
println("Started $op...")
}
Expand All @@ -24,6 +26,9 @@ class TestingInstrumentContext<T> implements InstrumentationContext<T> {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class TestingInstrumentation implements Instrumentation {
List<Class> dfClasses = []
def capturedData = [:]

def useOnDispatch = false

@Override
InstrumentationState createState() {
return instrumentationState
Expand All @@ -35,61 +37,61 @@ class TestingInstrumentation implements Instrumentation {
@Override
InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
new TestingInstrumentContext("execution", executionList, throwableList)
new TestingInstrumentContext("execution", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<Document> beginParse(InstrumentationExecutionParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("parse", executionList, throwableList)
return new TestingInstrumentContext("parse", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<List<ValidationError>> beginValidation(InstrumentationValidationParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("validation", executionList, throwableList)
return new TestingInstrumentContext("validation", executionList, throwableList, useOnDispatch)
}

@Override
ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingExecutionStrategyInstrumentationContext("execution-strategy", executionList, throwableList)
return new TestingExecutionStrategyInstrumentationContext("execution-strategy", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("execute-operation", executionList, throwableList)
return new TestingInstrumentContext("execute-operation", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<ExecutionResult> beginSubscribedFieldEvent(InstrumentationFieldParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("subscribed-field-event-$parameters.field.name", executionList, throwableList)
return new TestingInstrumentContext("subscribed-field-event-$parameters.field.name", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<ExecutionResult> beginField(InstrumentationFieldParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("field-$parameters.field.name", executionList, throwableList)
return new TestingInstrumentContext("field-$parameters.field.name", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("fetch-$parameters.field.name", executionList, throwableList)
return new TestingInstrumentContext("fetch-$parameters.field.name", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<CompletableFuture<ExecutionResult>> beginFieldComplete(InstrumentationFieldCompleteParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("complete-$parameters.field.name", executionList, throwableList)
return new TestingInstrumentContext("complete-$parameters.field.name", executionList, throwableList, useOnDispatch)
}

@Override
InstrumentationContext<CompletableFuture<ExecutionResult>> beginFieldListComplete(InstrumentationFieldCompleteParameters parameters) {
assert parameters.getInstrumentationState() == instrumentationState
return new TestingInstrumentContext("complete-list-$parameters.field.name", executionList, throwableList)
return new TestingInstrumentContext("complete-list-$parameters.field.name", executionList, throwableList, useOnDispatch)
}

@Override
Expand Down