Skip to content

Commit a08edb9

Browse files
authored
Instrument execution input so people can change that gets input (say variables etc..) (#803)
1 parent 56ce8cd commit a08edb9

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

src/main/java/graphql/ExecutionInput.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collections;
44
import java.util.Map;
5+
import java.util.function.Consumer;
56

67
/**
78
* This represents the series of values that can be input on a graphql query execution
@@ -58,6 +59,28 @@ public Map<String, Object> getVariables() {
5859
return variables;
5960
}
6061

62+
/**
63+
* This helps you transform the current ExecutionInput object into another one by starting a builder with all
64+
* the current values and allows you to transform it how you want.
65+
*
66+
* @param builderConsumer the consumer code that will be given a builder to transform
67+
*
68+
* @return a new ExecutionInput object based on calling build on that builder
69+
*/
70+
public ExecutionInput transform(Consumer<Builder> builderConsumer) {
71+
Builder builder = new Builder()
72+
.query(this.query)
73+
.operationName(this.operationName)
74+
.context(this.context)
75+
.root(this.root)
76+
.variables(this.variables);
77+
78+
builderConsumer.accept(builder);
79+
80+
return builder.build();
81+
}
82+
83+
6184
@Override
6285
public String toString() {
6386
return "ExecutionInput{" +

src/main/java/graphql/GraphQL.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
451451

452452
InstrumentationState instrumentationState = instrumentation.createState();
453453

454+
InstrumentationExecutionParameters inputInstrumentationParameters = new InstrumentationExecutionParameters(executionInput, this.graphQLSchema, instrumentationState);
455+
executionInput = instrumentation.instrumentExecutionInput(executionInput, inputInstrumentationParameters);
456+
454457
InstrumentationExecutionParameters instrumentationParameters = new InstrumentationExecutionParameters(executionInput, this.graphQLSchema, instrumentationState);
455458
InstrumentationContext<ExecutionResult> executionInstrumentation = instrumentation.beginExecution(instrumentationParameters);
456459

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.execution.instrumentation;
22

3+
import graphql.ExecutionInput;
34
import graphql.ExecutionResult;
45
import graphql.PublicApi;
56
import graphql.execution.Async;
@@ -165,6 +166,15 @@ public InstrumentationContext<CompletableFuture<ExecutionResult>> beginCompleteF
165166
.collect(toList()));
166167
}
167168

169+
@Override
170+
public ExecutionInput instrumentExecutionInput(ExecutionInput executionInput, InstrumentationExecutionParameters parameters) {
171+
for (Instrumentation instrumentation : instrumentations) {
172+
InstrumentationState state = getState(instrumentation, parameters.getInstrumentationState());
173+
executionInput = instrumentation.instrumentExecutionInput(executionInput, parameters.withNewState(state));
174+
}
175+
return executionInput;
176+
}
177+
168178
@Override
169179
public GraphQLSchema instrumentSchema(GraphQLSchema schema, InstrumentationExecutionParameters parameters) {
170180
for (Instrumentation instrumentation : instrumentations) {

src/main/java/graphql/execution/instrumentation/Instrumentation.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.execution.instrumentation;
22

3+
import graphql.ExecutionInput;
34
import graphql.ExecutionResult;
45
import graphql.execution.ExecutionContext;
56
import graphql.execution.instrumentation.parameters.InstrumentationDataFetchParameters;
@@ -171,14 +172,27 @@ default InstrumentationContext<CompletableFuture<ExecutionResult>> beginComplete
171172
};
172173
}
173174

175+
/**
176+
* This is called to instrument a {@link graphql.ExecutionInput} before it is used to parse, validate
177+
* and execute a query, allowing you to adjust what query input parameters are used
178+
*
179+
* @param executionInput the execution input to be used
180+
* @param parameters the parameters describing the field to be fetched
181+
*
182+
* @return a non null instrumented ExecutionInput, the default is to return to the same object
183+
*/
184+
default ExecutionInput instrumentExecutionInput(ExecutionInput executionInput, InstrumentationExecutionParameters parameters) {
185+
return executionInput;
186+
}
187+
174188
/**
175189
* This is called to instrument a {@link graphql.schema.GraphQLSchema} before it is used to parse, validate
176190
* and execute a query, allowing you to adjust what types are used.
177191
*
178192
* @param schema the schema to be used
179193
* @param parameters the parameters describing the field to be fetched
180194
*
181-
* @return a non null instrumented data fetcher, the default is to return to the same object
195+
* @return a non null instrumented GraphQLSchema, the default is to return to the same object
182196
*/
183197
default GraphQLSchema instrumentSchema(GraphQLSchema schema, InstrumentationExecutionParameters parameters) {
184198
return schema;
@@ -191,7 +205,7 @@ default GraphQLSchema instrumentSchema(GraphQLSchema schema, InstrumentationExec
191205
* @param executionContext the execution context to be used
192206
* @param parameters the parameters describing the field to be fetched
193207
*
194-
* @return a non null instrumented data fetcher, the default is to return to the same object
208+
* @return a non null instrumented ExecutionContext, the default is to return to the same object
195209
*/
196210
default ExecutionContext instrumentExecutionContext(ExecutionContext executionContext, InstrumentationExecutionParameters parameters) {
197211
return executionContext;
@@ -207,7 +221,7 @@ default ExecutionContext instrumentExecutionContext(ExecutionContext executionCo
207221
* @param dataFetcher the data fetcher about to be used
208222
* @param parameters the parameters describing the field to be fetched
209223
*
210-
* @return a non null instrumented data fetcher, the default is to return to the same object
224+
* @return a non null instrumented DataFetcher, the default is to return to the same object
211225
*/
212226
default DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
213227
return dataFetcher;

src/test/groovy/graphql/execution/instrumentation/TestingInstrumentation.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.execution.instrumentation
22

3+
import graphql.ExecutionInput
34
import graphql.ExecutionResult
45
import graphql.execution.ExecutionContext
56
import graphql.execution.instrumentation.parameters.InstrumentationDataFetchParameters
@@ -103,6 +104,12 @@ class TestingInstrumentation implements Instrumentation {
103104
return schema
104105
}
105106

107+
@Override
108+
ExecutionInput instrumentExecutionInput(ExecutionInput executionInput, InstrumentationExecutionParameters parameters) {
109+
assert parameters.getInstrumentationState() == instrumentationState
110+
return executionInput
111+
}
112+
106113
@Override
107114
ExecutionContext instrumentExecutionContext(ExecutionContext executionContext, InstrumentationExecutionParameters parameters) {
108115
assert parameters.getInstrumentationState() == instrumentationState

0 commit comments

Comments
 (0)