Skip to content

Commit

Permalink
PR graphql-java#217: Allow serialization of any Iterable collection a…
Browse files Browse the repository at this point in the history
…s a GraphQLList
  • Loading branch information
iancw authored and dminkovsky committed Oct 25, 2016
1 parent 12212bf commit 02ab468
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private ExecutionResult completeValueForList(ExecutionContext executionContext,
result = Arrays.asList((Object[]) result);
}

return completeValueForList(executionContext, fieldType, fields, (List<Object>) result);
return completeValueForList(executionContext, fieldType, fields, (Iterable<Object>) result);
}

protected GraphQLObjectType resolveType(GraphQLInterfaceType graphQLInterfaceType, Object value) {
Expand Down Expand Up @@ -126,7 +126,7 @@ protected ExecutionResult completeValueForScalar(GraphQLScalarType scalarType, O
return new ExecutionResultImpl(serialized, null);
}

protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, List<Object> result) {
protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, Iterable<Object> result) {
List<Object> completedResults = new ArrayList<Object>();
for (Object item : result) {
ExecutionResult completedValue = completeValue(executionContext, fieldType.getWrappedType(), fields, item);
Expand Down
27 changes: 23 additions & 4 deletions src/test/groovy/graphql/GraphQLTest.groovy
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package graphql

import graphql.language.SourceLocation
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLNonNull
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchema
import graphql.schema.*
import graphql.validation.ValidationErrorType
import spock.lang.Specification

Expand Down Expand Up @@ -149,4 +146,26 @@ class GraphQLTest extends Specification {
errors[0].validationErrorType == ValidationErrorType.MissingFieldArgument
errors[0].sourceLocations == [new SourceLocation(1, 3)]
}

def "`Iterable` can be used as a `GraphQLList` field result"() {
given:
def set = new HashSet<String>()
set.add("One")
set.add("Two")

def schema = GraphQLSchema.newSchema()
.query(GraphQLObjectType.newObject()
.name("QueryType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("set")
.type(new GraphQLList(GraphQLString))
.dataFetcher({ set })))
.build()

when:
def data = new GraphQL(schema).execute("query { set }").data

then:
data == [set: ['One', 'Two']]
}
}

0 comments on commit 02ab468

Please sign in to comment.