Skip to content

Commit ef7caf8

Browse files
authored
Merge pull request #377 from koblas/master
Fixed #355 handle null pointer lists
2 parents 5c1be08 + 6a8046d commit ef7caf8

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

executor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ func completeLeafValue(returnType Leaf, result interface{}) interface{} {
727727
// completeListValue complete a list value by completing each item in the list with the inner type
728728
func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) interface{} {
729729
resultVal := reflect.ValueOf(result)
730+
if resultVal.Kind() == reflect.Ptr {
731+
resultVal = resultVal.Elem()
732+
}
730733
parentTypeName := ""
731734
if info.ParentType != nil {
732735
parentTypeName = info.ParentType.Name()

lists_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,17 @@ func TestLists_ValueMayBeNilPointer(t *testing.T) {
899899
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
900900
}
901901
}
902+
903+
func TestLists_NullableListOfInt_ReturnsNull(t *testing.T) {
904+
ttype := graphql.NewList(graphql.Int)
905+
type dataType *[]int
906+
var data dataType
907+
expected := &graphql.Result{
908+
Data: map[string]interface{}{
909+
"nest": map[string]interface{}{
910+
"test": nil,
911+
},
912+
},
913+
}
914+
checkList(t, ttype, data, expected)
915+
}

union_interface_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,13 +627,8 @@ func TestUnionIntersectionTypes_ValueMayBeNilPointer(t *testing.T) {
627627
expected := &graphql.Result{
628628
Data: map[string]interface{}{
629629
"query": map[string]interface{}{
630-
"pet": map[string]interface{}{
631-
"__typename": "Cat",
632-
},
633-
"named": map[string]interface{}{
634-
"__typename": "Cat",
635-
"name": nil,
636-
},
630+
"pet": nil,
631+
"named": nil,
637632
}},
638633
}
639634
result := g(t, graphql.Params{

values.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ func isNullish(src interface{}) bool {
302302
}
303303
value := reflect.ValueOf(src)
304304
if value.Kind() == reflect.Ptr {
305+
if value.IsNil() {
306+
return true
307+
}
305308
value = value.Elem()
306309
}
307310
switch value.Kind() {
@@ -324,6 +327,9 @@ func isIterable(src interface{}) bool {
324327
return false
325328
}
326329
t := reflect.TypeOf(src)
330+
if t.Kind() == reflect.Ptr {
331+
t = t.Elem()
332+
}
327333
return t.Kind() == reflect.Slice || t.Kind() == reflect.Array
328334
}
329335

0 commit comments

Comments
 (0)