Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private Object unboxPossibleOptional(Object result) {
return result;
}

private Iterable<Object> toIterable(Object result) {
protected Iterable<Object> toIterable(Object result) {
if (result.getClass().isArray()) {
result = Arrays.asList((Object[]) result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private List<ExecutionNode> handleList(ExecutionContext executionContext, Map<St
}

MapOrList listResult = mapOrList.createAndPutList(fieldName);
for (Object rawValue : (List<Object>) value.getValue()) {
for (Object rawValue : toIterable(value.getValue())) {
flattenedValues.add(new FetchedValue(listResult, rawValue));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,12 @@ class BatchedExecutionStrategyTest extends Specification {
runTestBatching(query, expected)
runTestBatchingExpectErrors(query, new BatchAssertionFailed(), false)
}

def "Any Iterable is accepted as GraphQL list value"() {
given:
String query = "{ string(value: \"test\"){ anyIterable } }"
Map<String, Object> expected = ["string": ["anyIterable": ["test", "end"]]]
expect:
runTest(query, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import graphql.Scalars;
import graphql.schema.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class FunWithStringsSchemaFactory {
Expand Down Expand Up @@ -62,6 +60,19 @@ public Object get(DataFetchingEnvironment environment) {
}
});

factory.setAnyIterable(new DataFetcher() {
@Override
@Batched
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
List<Iterable<String>> retVal = new ArrayList<>();
for (String s: (List<String>) environment.getSource()) {
retVal.add(new LinkedHashSet<>(Arrays.asList(s, "end")));
}
return retVal;
}
});

factory.setAppendFetcher(new DataFetcher() {
@Override
@Batched
Expand Down Expand Up @@ -201,6 +212,11 @@ public Object get(DataFetchingEnvironment environment) {
return retVal;
};

public DataFetcher anyIterableFetcher = e -> {
String source = e.getSource();
return new LinkedHashSet<>(Arrays.asList(source, "end"));
};

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

public void setWordsAndLettersFetcher(DataFetcher fetcher) {
Expand Down Expand Up @@ -231,6 +247,10 @@ public void setReturnBadList(DataFetcher fetcher) {
this.returnBadListFetcher = fetcher;
}

public void setAnyIterable(DataFetcher fetcher) {
this.anyIterableFetcher = fetcher;
}

GraphQLSchema createSchema() {

GraphQLObjectType stringObjectType = GraphQLObjectType.newObject()
Expand All @@ -255,6 +275,10 @@ GraphQLSchema createSchema() {
.name("returnBadList")
.type(Scalars.GraphQLString)
.dataFetcher(returnBadListFetcher))
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("anyIterable")
.type(new GraphQLList(Scalars.GraphQLString))
.dataFetcher(anyIterableFetcher))
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("shatter")
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("StringObject")))))
Expand Down