Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executor treats arrays as iterable #327

Merged
merged 3 commits into from
Jul 2, 2018
Merged
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 executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*as
parentTypeName = info.ParentType.Name()
}
err := invariantf(
resultVal.IsValid() && resultVal.Type().Kind() == reflect.Slice,
resultVal.IsValid() && isIterable(result),
"User Error: expected iterable, but did not find one "+
"for field %v.%v.", parentTypeName, info.FieldName)

Expand Down
17 changes: 17 additions & 0 deletions lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,20 @@ func TestLists_UserErrorExpectIterableButDidNotGetOne(t *testing.T) {
}
checkList(t, ttype, data, expected)
}

func TestLists_ArrayOfNullableObjects_ContainsValues(t *testing.T) {
ttype := graphql.NewList(graphql.Int)
data := [2]interface{}{
1, 2,
}
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": []interface{}{
1, 2,
},
},
},
}
checkList(t, ttype, data, expected)
}
9 changes: 9 additions & 0 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ func isNullish(src interface{}) bool {
return false
}

// Returns true if src is a slice or an array
func isIterable(src interface{}) bool {
if src == nil {
return false
}
t := reflect.TypeOf(src)
return t.Kind() == reflect.Slice || t.Kind() == reflect.Array
}

/**
* Produces a value given a GraphQL Value AST.
*
Expand Down
18 changes: 18 additions & 0 deletions values_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package graphql

import "testing"

func TestIsIterable(t *testing.T) {
if !isIterable([]int{}) {
t.Fatal("expected isIterable to return true for a slice, got false")
}
if !isIterable([]int{}) {
t.Fatal("expected isIterable to return true for an array, got false")
}
if isIterable(1) {
t.Fatal("expected isIterable to return false for an int, got true")
}
if isIterable(nil) {
t.Fatal("expected isIterable to return false for nil, got true")
}
}