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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import graphql.ExperimentalApi;
import graphql.PublicApi;
import graphql.execution.Async;
import graphql.execution.DataFetcherResult;
import graphql.execution.ExecutionContext;
import graphql.execution.FieldValueInfo;
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters;
Expand Down Expand Up @@ -380,6 +381,11 @@ public void onFetchedValue(Object fetchedValue) {
contexts.forEach(context -> context.onFetchedValue(fetchedValue));
}

@Override
public void onExceptionHandled(DataFetcherResult<Object> dataFetcherResult) {
contexts.forEach(context -> context.onExceptionHandled(dataFetcherResult));
}

@Override
public void onCompleted(Object result, Throwable t) {
contexts.forEach(context -> context.onCompleted(result, t));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import graphql.execution.AsyncExecutionStrategy
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import graphql.execution.instrumentation.parameters.InstrumentationValidationParameters
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.validation.ValidationError
import spock.lang.Specification

Expand Down Expand Up @@ -227,6 +229,78 @@ class ChainedInstrumentationStateTest extends Specification {
assertCalls(c)
}

def "basic chaining and state management when exception raised in data fetching"() {

def a = new NamedInstrumentation("A")
def b = new NamedInstrumentation("B")
def c = new NamedInstrumentation("C")
def nullState = new SimplePerformantInstrumentation()

def chainedInstrumentation = new ChainedInstrumentation([
a,
b,
nullState,
c,
])

def query = """
query HeroNameAndFriendsQuery {
hero {
id
}
}
"""

def expected = "onExceptionHandled:fetch-id"


when:
def strategy = new AsyncExecutionStrategy()
def schema = StarWarsSchema.starWarsSchema
def graphQL = GraphQL
.newGraphQL(schema.transform { schemaBuilder ->
// throw exception when fetching the hero id
def exceptionDataFetcher = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) {
throw new RuntimeException("Data fetcher exception")
}
}
schemaBuilder.codeRegistry(schema.codeRegistry.transform {
it.dataFetcher(
schema.getObjectType("Human"),
schema.getObjectType("Human").getFieldDefinition("id"),
exceptionDataFetcher
)
it.dataFetcher(
schema.getObjectType("Droid"),
schema.getObjectType("Droid").getFieldDefinition("id"),
exceptionDataFetcher
)
return it
}
)
})
.queryExecutionStrategy(strategy)
.instrumentation(chainedInstrumentation)
.build()

graphQL.execute(query)

then:

chainedInstrumentation.getInstrumentations().size() == 4

a.executionList.any { it == expected }
b.executionList.any { it == expected }
c.executionList.any { it == expected }

assertCalls(a)
assertCalls(b)
assertCalls(c)

}

def "empty chain"() {
def chainedInstrumentation = new ChainedInstrumentation(Arrays.asList())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package graphql.execution.instrumentation

import graphql.execution.DataFetcherResult

class TestingFieldFetchingInstrumentationContext extends TestingInstrumentContext<Object> implements FieldFetchingInstrumentationContext {

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

@Override
void onExceptionHandled(DataFetcherResult<Object> dataFetcherResult) {
executionList << "onExceptionHandled:$op"
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package graphql.execution.instrumentation

import java.util.concurrent.CompletableFuture

class TestingInstrumentContext<T> implements InstrumentationContext<T> {
def op
def start = System.currentTimeMillis()
Expand Down
Loading