1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
33
33
34
34
/**
35
35
* Common representation for GraphQL request input. This can be converted to
36
- * {@link ExecutionInput} via {@link #toExecutionInput()} and the
36
+ * {@link ExecutionInput} via {@link #toExecutionInput(boolean )} and the
37
37
* {@code ExecutionInput} further customized via
38
38
* {@link #configureExecutionInput(BiFunction)}.
39
39
*
@@ -57,6 +57,8 @@ public class RequestInput {
57
57
58
58
private final List <BiFunction <ExecutionInput , ExecutionInput .Builder , ExecutionInput >> executionInputConfigurers = new ArrayList <>();
59
59
60
+ @ Nullable
61
+ private ExecutionId executionId ;
60
62
61
63
/**
62
64
* Create an instance.
@@ -80,14 +82,9 @@ public RequestInput(
80
82
}
81
83
82
84
83
- @ SuppressWarnings ("unchecked" )
84
- private static <T > T getKey (String key , Map <String , Object > body ) {
85
- return (T ) body .get (key );
86
- }
87
-
88
85
/**
89
- * Return an identifier for the request. This id is later propagated
90
- * as the {@link ExecutionId} of the execution input .
86
+ * Return an identifier for the request. This id can be later propagated
87
+ * as the {@link ExecutionId} if {@link #executionId(ExecutionId) none has been set} .
91
88
* <p>For web transports, this identifier can be used to correlate
92
89
* request and response messages on a multiplexed connection.
93
90
* @return the request id.
@@ -131,6 +128,15 @@ public Locale getLocale() {
131
128
return this .locale ;
132
129
}
133
130
131
+ /**
132
+ * Set an {@link ExecutionId} to be used for the {@link ExecutionInput}
133
+ * @param executionId the execution id to use with the {@link ExecutionInput}.
134
+ */
135
+ public void executionId (ExecutionId executionId ) {
136
+ Assert .notNull (executionId , "executionId should not be null" );
137
+ this .executionId = executionId ;
138
+ }
139
+
134
140
/**
135
141
* Provide a consumer to configure the {@link ExecutionInput} used for input to
136
142
* {@link graphql.GraphQL#executeAsync(ExecutionInput)}. The builder is initially
@@ -148,16 +154,22 @@ public void configureExecutionInput(BiFunction<ExecutionInput, ExecutionInput.Bu
148
154
* populated from {@link #getQuery()}, {@link #getOperationName()}, and
149
155
* {@link #getVariables()}, and is then further customized through
150
156
* {@link #configureExecutionInput(BiFunction)}.
157
+ * @param useRequestId whether the {@link #getId()} should be used as a fallback for {@link ExecutionId}.
151
158
* @return the execution input
152
159
*/
153
- public ExecutionInput toExecutionInput () {
154
- ExecutionInput executionInput = ExecutionInput .newExecutionInput ()
160
+ public ExecutionInput toExecutionInput (boolean useRequestId ) {
161
+ ExecutionInput . Builder inputBuilder = ExecutionInput .newExecutionInput ()
155
162
.query (this .query )
156
163
.operationName (this .operationName )
157
164
.variables (this .variables )
158
- .locale (this .locale )
159
- .executionId (ExecutionId .from (this .id ))
160
- .build ();
165
+ .locale (this .locale );
166
+ if (this .executionId != null ) {
167
+ inputBuilder .executionId (this .executionId );
168
+ }
169
+ else if (useRequestId ) {
170
+ inputBuilder .executionId (ExecutionId .from (this .id ));
171
+ }
172
+ ExecutionInput executionInput = inputBuilder .build ();
161
173
162
174
for (BiFunction <ExecutionInput , ExecutionInput .Builder , ExecutionInput > configurer : this .executionInputConfigurers ) {
163
175
ExecutionInput current = executionInput ;
0 commit comments