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

Fixed #355 handle null pointer lists #377

Merged
merged 1 commit into from Aug 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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