Skip to content

Commit 9a61bfe

Browse files
jmccaullbbakerman
authored andcommitted
added the ability to set execution id within ExecutionInput
1 parent ca06d0e commit 9a61bfe

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

src/main/java/graphql/ExecutionInput.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql;
22

33
import graphql.cachecontrol.CacheControl;
4+
import graphql.execution.ExecutionId;
45
import org.dataloader.DataLoaderRegistry;
56

67
import java.util.Collections;
@@ -22,21 +23,23 @@ public class ExecutionInput {
2223
private final Map<String, Object> variables;
2324
private final DataLoaderRegistry dataLoaderRegistry;
2425
private final CacheControl cacheControl;
26+
private final ExecutionId executionId;
2527

2628

2729
public ExecutionInput(String query, String operationName, Object context, Object root, Map<String, Object> variables) {
28-
this(query, operationName, context, root, variables, new DataLoaderRegistry(), null);
30+
this(query, operationName, context, root, variables, new DataLoaderRegistry(), null, null);
2931
}
3032

3133
@Internal
32-
private ExecutionInput(String query, String operationName, Object context, Object root, Map<String, Object> variables, DataLoaderRegistry dataLoaderRegistry, CacheControl cacheControl) {
34+
private ExecutionInput(String query, String operationName, Object context, Object root, Map<String, Object> variables, DataLoaderRegistry dataLoaderRegistry, CacheControl cacheControl, ExecutionId executionId) {
3335
this.query = query;
3436
this.operationName = operationName;
3537
this.context = context;
3638
this.root = root;
3739
this.variables = variables;
3840
this.dataLoaderRegistry = dataLoaderRegistry;
3941
this.cacheControl = cacheControl;
42+
this.executionId = executionId;
4043
}
4144

4245
/**
@@ -88,23 +91,30 @@ public CacheControl getCacheControl() {
8891
return cacheControl;
8992
}
9093

94+
/**
95+
* @return Id that will be/was used to execute this operation.
96+
*/
97+
public ExecutionId getExecutionId() {
98+
return executionId;
99+
}
100+
91101
/**
92102
* This helps you transform the current ExecutionInput object into another one by starting a builder with all
93103
* the current values and allows you to transform it how you want.
94104
*
95105
* @param builderConsumer the consumer code that will be given a builder to transform
96-
*
97106
* @return a new ExecutionInput object based on calling build on that builder
98107
*/
99108
public ExecutionInput transform(Consumer<Builder> builderConsumer) {
100109
Builder builder = new Builder()
101-
.query(this.query)
102-
.operationName(this.operationName)
103-
.context(this.context)
104-
.root(this.root)
105-
.dataLoaderRegistry(this.dataLoaderRegistry)
106-
.cacheControl(this.cacheControl)
107-
.variables(this.variables);
110+
.query(this.query)
111+
.operationName(this.operationName)
112+
.context(this.context)
113+
.root(this.root)
114+
.dataLoaderRegistry(this.dataLoaderRegistry)
115+
.cacheControl(this.cacheControl)
116+
.variables(this.variables)
117+
.executionId(executionId);
108118

109119
builderConsumer.accept(builder);
110120

@@ -115,13 +125,14 @@ public ExecutionInput transform(Consumer<Builder> builderConsumer) {
115125
@Override
116126
public String toString() {
117127
return "ExecutionInput{" +
118-
"query='" + query + '\'' +
119-
", operationName='" + operationName + '\'' +
120-
", context=" + context +
121-
", root=" + root +
122-
", variables=" + variables +
123-
", dataLoaderRegistry=" + dataLoaderRegistry +
124-
'}';
128+
"query='" + query + '\'' +
129+
", operationName='" + operationName + '\'' +
130+
", context=" + context +
131+
", root=" + root +
132+
", variables=" + variables +
133+
", dataLoaderRegistry=" + dataLoaderRegistry +
134+
", executionId= " + executionId +
135+
'}';
125136
}
126137

127138
/**
@@ -135,7 +146,6 @@ public static Builder newExecutionInput() {
135146
* Creates a new builder of ExecutionInput objects with the given query
136147
*
137148
* @param query the query to execute
138-
*
139149
* @return a new builder of ExecutionInput objects
140150
*/
141151
public static Builder newExecutionInput(String query) {
@@ -151,6 +161,7 @@ public static class Builder {
151161
private Map<String, Object> variables = Collections.emptyMap();
152162
private DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
153163
private CacheControl cacheControl = CacheControl.newCacheControl();
164+
private ExecutionId executionId = null;
154165

155166
public Builder query(String query) {
156167
this.query = query;
@@ -162,11 +173,20 @@ public Builder operationName(String operationName) {
162173
return this;
163174
}
164175

176+
/**
177+
* A default one will be assigned, but you can set your own.
178+
* @param executionId an execution id object
179+
* @return this builder
180+
*/
181+
public Builder executionId(ExecutionId executionId) {
182+
this.executionId = executionId;
183+
return this;
184+
}
185+
165186
/**
166187
* By default you will get a {@link GraphQLContext} object but you can set your own.
167188
*
168189
* @param context the context object to use
169-
*
170190
* @return this builder
171191
*/
172192
public Builder context(Object context) {
@@ -196,11 +216,11 @@ public Builder variables(Map<String, Object> variables) {
196216
}
197217

198218
/**
199-
* You should create new {@link org.dataloader.DataLoaderRegistry}s and new {@link org.dataloader.DataLoader}s for each execution. Do not re-use
219+
* You should create new {@link org.dataloader.DataLoaderRegistry}s and new {@link org.dataloader.DataLoader}s for each execution. Do not
220+
* re-use
200221
* instances as this will create unexpected results.
201222
*
202223
* @param dataLoaderRegistry a registry of {@link org.dataloader.DataLoader}s
203-
*
204224
* @return this builder
205225
*/
206226
public Builder dataLoaderRegistry(DataLoaderRegistry dataLoaderRegistry) {
@@ -214,7 +234,7 @@ public Builder cacheControl(CacheControl cacheControl) {
214234
}
215235

216236
public ExecutionInput build() {
217-
return new ExecutionInput(query, operationName, context, root, variables, dataLoaderRegistry, cacheControl);
237+
return new ExecutionInput(query, operationName, context, root, variables, dataLoaderRegistry, cacheControl, executionId);
218238
}
219239
}
220-
}
240+
}

src/main/java/graphql/GraphQL.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ public CompletableFuture<ExecutionResult> executeAsync(UnaryOperator<ExecutionIn
482482
public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionInput) {
483483
try {
484484
log.debug("Executing request. operation name: '{}'. query: '{}'. variables '{}'", executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());
485+
executionInput = ensureInputHasId(executionInput);
485486

486487
InstrumentationState instrumentationState = instrumentation.createState(new InstrumentationCreateStateParameters(this.graphQLSchema, executionInput));
487488

@@ -506,6 +507,16 @@ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionI
506507
}
507508
}
508509

510+
private ExecutionInput ensureInputHasId(ExecutionInput executionInput) {
511+
if (executionInput.getExecutionId() != null) {
512+
return executionInput;
513+
}
514+
String queryString = executionInput.getQuery();
515+
String operationName = executionInput.getOperationName();
516+
Object context = executionInput.getContext();
517+
return executionInput.transform(builder -> builder.executionId(idProvider.provide(queryString, operationName, context)));
518+
}
519+
509520

510521
private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
511522
AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput);
@@ -581,12 +592,9 @@ private List<ValidationError> validate(ExecutionInput executionInput, Document d
581592
}
582593

583594
private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
584-
String query = executionInput.getQuery();
585-
String operationName = executionInput.getOperationName();
586-
Object context = executionInput.getContext();
587595

588596
Execution execution = new Execution(queryStrategy, mutationStrategy, subscriptionStrategy, instrumentation);
589-
ExecutionId executionId = idProvider.provide(query, operationName, context);
597+
ExecutionId executionId = executionInput.getExecutionId();
590598

591599
log.debug("Executing '{}'. operation name: '{}'. query: '{}'. variables '{}'", executionId, executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());
592600
CompletableFuture<ExecutionResult> future = execution.execute(document, graphQLSchema, executionId, executionInput, instrumentationState);

0 commit comments

Comments
 (0)