Skip to content

Commit 9be7527

Browse files
committed
Propagate nullable chained instrumentation state
1 parent ac661d0 commit 9be7527

4 files changed

Lines changed: 95 additions & 22 deletions

File tree

‎src/main/java/graphql/execution/instrumentation/ChainedInstrumentation.java‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static ChainedInstrumentationState asChainedState(@Nullable Instrumentat
7676
return (ChainedInstrumentationState) assertNotNull(state);
7777
}
7878

79-
private <T> @Nullable InstrumentationContext<T> chainedCtx(@Nullable InstrumentationState state, BiFunction<Instrumentation, InstrumentationState, InstrumentationContext<T>> mapper) {
79+
private <T> @Nullable InstrumentationContext<T> chainedCtx(@Nullable InstrumentationState state, BiFunction<Instrumentation, @Nullable InstrumentationState, InstrumentationContext<T>> mapper) {
8080
// if we have zero or 1 instrumentations (and 1 is the most common), then we can avoid an object allocation
8181
// of the ChainedInstrumentationContext since it won't be needed
8282
if (instrumentations.isEmpty()) {
@@ -89,35 +89,35 @@ private static ChainedInstrumentationState asChainedState(@Nullable Instrumentat
8989
return new ChainedInstrumentationContext<>(chainedMapAndDropNulls(chainedInstrumentationState, mapper));
9090
}
9191

92-
private <T> T chainedInstrument(@Nullable InstrumentationState state, T input, ChainedInstrumentationFunction<Instrumentation, InstrumentationState, T, T> mapper) {
92+
private <T> T chainedInstrument(@Nullable InstrumentationState state, T input, ChainedInstrumentationFunction<Instrumentation, T, T> mapper) {
9393
ChainedInstrumentationState chainedInstrumentationState = asChainedState(state);
9494
for (int i = 0; i < instrumentations.size(); i++) {
9595
Instrumentation instrumentation = instrumentations.get(i);
96-
InstrumentationState specificState = chainedInstrumentationState.getState(i);
96+
@Nullable InstrumentationState specificState = chainedInstrumentationState.getState(i);
9797
input = mapper.apply(instrumentation, specificState, input);
9898
}
9999
return input;
100100
}
101101

102-
protected <T> ImmutableList<T> chainedMapAndDropNulls(@Nullable InstrumentationState state, BiFunction<Instrumentation, InstrumentationState, T> mapper) {
102+
protected <T> ImmutableList<T> chainedMapAndDropNulls(@Nullable InstrumentationState state, BiFunction<Instrumentation, @Nullable InstrumentationState, T> mapper) {
103103
ChainedInstrumentationState chainedInstrumentationState = asChainedState(state);
104104
ImmutableList.Builder<T> result = ImmutableList.builderWithExpectedSize(instrumentations.size());
105105
for (int i = 0; i < instrumentations.size(); i++) {
106106
Instrumentation instrumentation = instrumentations.get(i);
107-
InstrumentationState specificState = chainedInstrumentationState.getState(i);
108-
T value = mapper.apply(instrumentation, specificState);
107+
@Nullable InstrumentationState specificState = chainedInstrumentationState.getState(i);
108+
@Nullable T value = mapper.apply(instrumentation, specificState);
109109
if (value != null) {
110110
result.add(value);
111111
}
112112
}
113113
return result.build();
114114
}
115115

116-
protected void chainedConsume(@Nullable InstrumentationState state, BiConsumer<Instrumentation, InstrumentationState> stateConsumer) {
116+
protected void chainedConsume(@Nullable InstrumentationState state, BiConsumer<Instrumentation, @Nullable InstrumentationState> stateConsumer) {
117117
ChainedInstrumentationState chainedInstrumentationState = asChainedState(state);
118118
for (int i = 0; i < instrumentations.size(); i++) {
119119
Instrumentation instrumentation = instrumentations.get(i);
120-
InstrumentationState specificState = chainedInstrumentationState.getState(i);
120+
@Nullable InstrumentationState specificState = chainedInstrumentationState.getState(i);
121121
stateConsumer.accept(instrumentation, specificState);
122122
}
123123
}
@@ -159,7 +159,7 @@ public CompletableFuture<InstrumentationState> createStateAsync(InstrumentationC
159159
if (instrumentations.isEmpty()) {
160160
return ExecutionStrategyInstrumentationContext.NOOP;
161161
}
162-
BiFunction<Instrumentation, InstrumentationState, ExecutionStrategyInstrumentationContext> mapper = (instrumentation, specificState) -> instrumentation.beginExecutionStrategy(parameters, specificState);
162+
BiFunction<Instrumentation, @Nullable InstrumentationState, ExecutionStrategyInstrumentationContext> mapper = (instrumentation, specificState) -> instrumentation.beginExecutionStrategy(parameters, specificState);
163163
ChainedInstrumentationState chainedInstrumentationState = asChainedState(state);
164164
if (instrumentations.size() == 1) {
165165
return mapper.apply(instrumentations.get(0), chainedInstrumentationState.getState(0));
@@ -172,7 +172,7 @@ public CompletableFuture<InstrumentationState> createStateAsync(InstrumentationC
172172
if (instrumentations.isEmpty()) {
173173
return ExecuteObjectInstrumentationContext.NOOP;
174174
}
175-
BiFunction<Instrumentation, InstrumentationState, ExecuteObjectInstrumentationContext> mapper = (instrumentation, specificState) -> instrumentation.beginExecuteObject(parameters, specificState);
175+
BiFunction<Instrumentation, @Nullable InstrumentationState, ExecuteObjectInstrumentationContext> mapper = (instrumentation, specificState) -> instrumentation.beginExecuteObject(parameters, specificState);
176176
ChainedInstrumentationState chainedInstrumentationState = asChainedState(state);
177177
if (instrumentations.size() == 1) {
178178
return mapper.apply(instrumentations.get(0), chainedInstrumentationState.getState(0));
@@ -207,7 +207,7 @@ public CompletableFuture<InstrumentationState> createStateAsync(InstrumentationC
207207
if (instrumentations.isEmpty()) {
208208
return FieldFetchingInstrumentationContext.NOOP;
209209
}
210-
BiFunction<Instrumentation, InstrumentationState, FieldFetchingInstrumentationContext> mapper = (instrumentation, specificState) -> instrumentation.beginFieldFetching(parameters, specificState);
210+
BiFunction<Instrumentation, @Nullable InstrumentationState, FieldFetchingInstrumentationContext> mapper = (instrumentation, specificState) -> instrumentation.beginFieldFetching(parameters, specificState);
211211
ChainedInstrumentationState chainedInstrumentationState = asChainedState(state);
212212
if (instrumentations.size() == 1) {
213213
return mapper.apply(instrumentations.get(0), chainedInstrumentationState.getState(0));
@@ -252,38 +252,38 @@ public ExecutionContext instrumentExecutionContext(ExecutionContext executionCon
252252

253253
@Override
254254
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters, @Nullable InstrumentationState state) {
255-
return chainedInstrument(state, dataFetcher, (Instrumentation instrumentation, InstrumentationState specificState, DataFetcher<?> accumulator) ->
255+
return chainedInstrument(state, dataFetcher, (Instrumentation instrumentation, @Nullable InstrumentationState specificState, DataFetcher<?> accumulator) ->
256256
instrumentation.instrumentDataFetcher(accumulator, parameters, specificState));
257257
}
258258

259259
@Override
260260
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters, @Nullable InstrumentationState state) {
261-
ImmutableList<Map.Entry<Instrumentation, InstrumentationState>> entries = chainedMapAndDropNulls(state, AbstractMap.SimpleEntry::new);
261+
ImmutableList<Map.Entry<Instrumentation, @Nullable InstrumentationState>> entries = chainedMapAndDropNulls(state, AbstractMap.SimpleEntry::new);
262262
CompletableFuture<List<ExecutionResult>> resultsFuture = Async.eachSequentially(entries, (entry, prevResults) -> {
263263
Instrumentation instrumentation = entry.getKey();
264-
InstrumentationState specificState = entry.getValue();
264+
@Nullable InstrumentationState specificState = entry.getValue();
265265
ExecutionResult lastResult = !prevResults.isEmpty() ? prevResults.get(prevResults.size() - 1) : executionResult;
266266
return instrumentation.instrumentExecutionResult(lastResult, parameters, specificState);
267267
});
268268
return resultsFuture.thenApply((results) -> results.isEmpty() ? executionResult : results.get(results.size() - 1));
269269
}
270270

271271
static class ChainedInstrumentationState implements InstrumentationState {
272-
private final List<InstrumentationState> instrumentationStates;
272+
private final List<@Nullable InstrumentationState> instrumentationStates;
273273

274-
private ChainedInstrumentationState(List<InstrumentationState> instrumentationStates) {
274+
private ChainedInstrumentationState(List<@Nullable InstrumentationState> instrumentationStates) {
275275
this.instrumentationStates = instrumentationStates;
276276
}
277277

278-
private InstrumentationState getState(int index) {
278+
private @Nullable InstrumentationState getState(int index) {
279279
return instrumentationStates.get(index);
280280
}
281281

282282
private static CompletableFuture<InstrumentationState> combineAll(List<Instrumentation> instrumentations, InstrumentationCreateStateParameters parameters) {
283-
Async.CombinedBuilder<InstrumentationState> builder = Async.ofExpectedSize(instrumentations.size());
283+
Async.CombinedBuilder<@Nullable InstrumentationState> builder = Async.ofExpectedSize(instrumentations.size());
284284
for (Instrumentation instrumentation : instrumentations) {
285285
// state can be null including the CF so handle that
286-
CompletableFuture<InstrumentationState> stateCF = Async.orNullCompletedFuture(instrumentation.createStateAsync(parameters));
286+
CompletableFuture<@Nullable InstrumentationState> stateCF = Async.<@Nullable InstrumentationState>orNullCompletedFuture(instrumentation.createStateAsync(parameters));
287287
builder.add(stateCF);
288288
}
289289
return builder.await().thenApply(ChainedInstrumentationState::new);
@@ -417,8 +417,8 @@ public void onCompleted(@Nullable Object result, @Nullable Throwable t) {
417417
}
418418

419419
@FunctionalInterface
420-
private interface ChainedInstrumentationFunction<I, S, V, R> {
421-
R apply(I instrumentation, S state, V value);
420+
private interface ChainedInstrumentationFunction<I, V, R> {
421+
R apply(I instrumentation, @Nullable InstrumentationState state, V value);
422422
}
423423

424424

‎src/main/java/graphql/execution/instrumentation/NoContextChainedInstrumentation.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public NoContextChainedInstrumentation(Instrumentation... instrumentations) {
5151
super(instrumentations);
5252
}
5353

54-
private <T> @Nullable T runAll(@Nullable InstrumentationState state, BiConsumer<Instrumentation, InstrumentationState> stateConsumer) {
54+
private <T> @Nullable T runAll(@Nullable InstrumentationState state, BiConsumer<Instrumentation, @Nullable InstrumentationState> stateConsumer) {
5555
chainedConsume(state, stateConsumer);
5656
return null;
5757
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package graphql.execution.instrumentation
2+
3+
import graphql.GraphQL
4+
import graphql.schema.idl.RuntimeWiring
5+
import graphql.schema.idl.SchemaGenerator
6+
import graphql.schema.idl.SchemaParser
7+
import spock.lang.Specification
8+
9+
class InstrumentationStateNullabilityTest extends Specification {
10+
11+
def "stateless Kotlin instrumentation executes with null state"() {
12+
given:
13+
def fixtureClass = Class.forName("graphql.execution.instrumentation.KotlinStatelessInstrumentation")
14+
def instrumentation = fixtureClass.getDeclaredConstructor().newInstance() as Instrumentation
15+
def typeRegistry = new SchemaParser().parse("""
16+
type Query {
17+
hello: String
18+
}
19+
""")
20+
def schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, RuntimeWiring.MOCKED_WIRING)
21+
def graphQL = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build()
22+
23+
when:
24+
def result = graphQL.execute("{ hello }")
25+
26+
then:
27+
result.errors.isEmpty()
28+
fixtureClass.getMethod("getSeenStates").invoke(instrumentation) == [null]
29+
}
30+
31+
def "Kotlin chained instrumentation receives nullable child state"() {
32+
given:
33+
def fixtureClass = Class.forName("graphql.execution.instrumentation.KotlinChainedInstrumentation")
34+
def instrumentation = fixtureClass
35+
.getDeclaredConstructor(Instrumentation)
36+
.newInstance(SimplePerformantInstrumentation.INSTANCE) as ChainedInstrumentation
37+
def state = instrumentation.createStateAsync(null).join()
38+
39+
when:
40+
def childState = fixtureClass
41+
.getMethod("consumeChildState", InstrumentationState)
42+
.invoke(instrumentation, state)
43+
44+
then:
45+
childState == null
46+
}
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package graphql.execution.instrumentation
2+
3+
import graphql.ExecutionResult
4+
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
5+
6+
class KotlinStatelessInstrumentation : SimplePerformantInstrumentation() {
7+
val seenStates = mutableListOf<InstrumentationState?>()
8+
9+
override fun beginExecution(
10+
parameters: InstrumentationExecutionParameters,
11+
state: InstrumentationState?,
12+
): InstrumentationContext<ExecutionResult> {
13+
seenStates.add(state)
14+
return SimpleInstrumentationContext.noOp()
15+
}
16+
}
17+
18+
class KotlinChainedInstrumentation(
19+
instrumentation: Instrumentation,
20+
) : ChainedInstrumentation(instrumentation) {
21+
fun consumeChildState(state: InstrumentationState?): InstrumentationState? {
22+
var observedState: InstrumentationState? = null
23+
chainedConsume(state) { _, childState -> observedState = childState }
24+
return observedState
25+
}
26+
}

0 commit comments

Comments
 (0)