Skip to content

Commit

Permalink
Merge pull request #377 from koblas/master
Browse files Browse the repository at this point in the history
Fixed #355 handle null pointer lists
  • Loading branch information
chris-ramon committed Aug 21, 2018
2 parents 5c1be08 + 6a8046d commit ef7caf8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
3 changes: 3 additions & 0 deletions executor.go
Expand Up @@ -727,6 +727,9 @@ func completeLeafValue(returnType Leaf, result interface{}) interface{} {
// completeListValue complete a list value by completing each item in the list with the inner type
func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) interface{} {
resultVal := reflect.ValueOf(result)
if resultVal.Kind() == reflect.Ptr {
resultVal = resultVal.Elem()
}
parentTypeName := ""
if info.ParentType != nil {
parentTypeName = info.ParentType.Name()
Expand Down
14 changes: 14 additions & 0 deletions lists_test.go
Expand Up @@ -899,3 +899,17 @@ func TestLists_ValueMayBeNilPointer(t *testing.T) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestLists_NullableListOfInt_ReturnsNull(t *testing.T) {
ttype := graphql.NewList(graphql.Int)
type dataType *[]int
var data dataType
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": nil,
},
},
}
checkList(t, ttype, data, expected)
}
9 changes: 2 additions & 7 deletions union_interface_test.go
Expand Up @@ -627,13 +627,8 @@ func TestUnionIntersectionTypes_ValueMayBeNilPointer(t *testing.T) {
expected := &graphql.Result{
Data: map[string]interface{}{
"query": map[string]interface{}{
"pet": map[string]interface{}{
"__typename": "Cat",
},
"named": map[string]interface{}{
"__typename": "Cat",
"name": nil,
},
"pet": nil,
"named": nil,
}},
}
result := g(t, graphql.Params{
Expand Down
6 changes: 6 additions & 0 deletions values.go
Expand Up @@ -302,6 +302,9 @@ func isNullish(src interface{}) bool {
}
value := reflect.ValueOf(src)
if value.Kind() == reflect.Ptr {
if value.IsNil() {
return true
}
value = value.Elem()
}
switch value.Kind() {
Expand All @@ -324,6 +327,9 @@ func isIterable(src interface{}) bool {
return false
}
t := reflect.TypeOf(src)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.Kind() == reflect.Slice || t.Kind() == reflect.Array
}

Expand Down

0 comments on commit ef7caf8

Please sign in to comment.