Skip to content

Commit b31319d

Browse files
authored
Adds Optional support to BatchedES (graphql-java#678)
1 parent 97593b7 commit b31319d

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec
389389
*
390390
* @return an un-boxed result
391391
*/
392-
private Object unboxPossibleOptional(Object result) {
392+
protected Object unboxPossibleOptional(Object result) {
393393
if (result instanceof Optional) {
394394
Optional optional = (Optional) result;
395395
if (optional.isPresent()) {

‎src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ private List<ExecutionNode> handleList(ExecutionContext executionContext, Map<St
202202

203203
MapOrList listResult = mapOrList.createAndPutList(fieldName);
204204
for (Object rawValue : (List<Object>) value.getValue()) {
205+
rawValue = unboxPossibleOptional(rawValue);
205206
flattenedValues.add(new FetchedValue(listResult, rawValue));
206207
}
207208
}
@@ -408,7 +409,8 @@ private BiFunction<List<Object>, Throwable, List<FetchedValue>> handleResult(Exe
408409
List<Object> values = result;
409410
List<FetchedValue> retVal = new ArrayList<>();
410411
for (int i = 0; i < parentResults.size(); i++) {
411-
retVal.add(new FetchedValue(parentResults.get(i), values.get(i)));
412+
Object value = unboxPossibleOptional(values.get(i));
413+
retVal.add(new FetchedValue(parentResults.get(i), value));
412414
}
413415
return retVal;
414416
};

‎src/test/groovy/graphql/execution/batched/BatchedExecutionStrategyTest.groovy‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,16 @@ class BatchedExecutionStrategyTest extends Specification {
379379
runTestBatching(query, expected)
380380
runTestBatchingExpectErrors(query, new BatchAssertionFailed(), false)
381381
}
382+
383+
def "#673 optional support"() {
384+
given:
385+
String query = "{ string(value: \"673-optional-support\"){emptyOptional, optional} }"
386+
387+
def expected = [string: [emptyOptional:null, optional:"673-optional-support"]]
388+
println expected
389+
390+
expect:
391+
runTest(query, expected)
392+
}
393+
382394
}

‎src/test/groovy/graphql/execution/batched/FunWithStringsSchemaFactory.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Optional;
910
import java.util.concurrent.atomic.AtomicInteger;
1011

1112
public class FunWithStringsSchemaFactory {
@@ -203,6 +204,10 @@ public Object get(DataFetchingEnvironment environment) {
203204

204205
public DataFetcher appendFetcher = e -> ((String)e.getSource()) + e.getArgument("text");
205206

207+
public DataFetcher emptyOptionalFetcher = e -> Optional.empty();
208+
209+
public DataFetcher optionalFetcher = e -> Optional.of("673-optional-support");
210+
206211
public void setWordsAndLettersFetcher(DataFetcher fetcher) {
207212
this.wordsAndLettersFetcher = fetcher;
208213
}
@@ -292,6 +297,22 @@ GraphQLSchema createSchema() {
292297
.type(Scalars.GraphQLString))
293298
.dataFetcher(appendFetcher))
294299

300+
.field(GraphQLFieldDefinition.newFieldDefinition()
301+
.name("emptyOptional")
302+
.type(Scalars.GraphQLString)
303+
.argument(GraphQLArgument.newArgument()
304+
.name("text")
305+
.type(Scalars.GraphQLString))
306+
.dataFetcher(emptyOptionalFetcher))
307+
308+
.field(GraphQLFieldDefinition.newFieldDefinition()
309+
.name("optional")
310+
.type(Scalars.GraphQLString)
311+
.argument(GraphQLArgument.newArgument()
312+
.name("text")
313+
.type(Scalars.GraphQLString))
314+
.dataFetcher(optionalFetcher))
315+
295316
.build();
296317

297318

0 commit comments

Comments
 (0)