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

null lists of non primitive objects are not handled #355

Closed
koblas opened this issue Jul 16, 2018 · 1 comment
Closed

null lists of non primitive objects are not handled #355

koblas opened this issue Jul 16, 2018 · 1 comment

Comments

@koblas
Copy link
Contributor

koblas commented Jul 16, 2018

The following unit test demonstrates the problem:

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)
}

Expected functionality is that "test" is nil, instead, an error is generated:

	got: [User Error: expected iterable, but did not find one for field DataType.test.]
	want: []

The problem is that isNullish() doesn't look at the interface pointer types. Potentially you should do a:

if !value.IsValid() {
   return false
} 

before the switch statement.

@koblas
Copy link
Contributor Author

koblas commented Jul 16, 2018

A few more fixes are also needed:

values.go

// Returns true if a value is null, undefined, or NaN.
func isNullish(src interface{}) bool {
    if src == nil {
        return true
    }
    value := reflect.ValueOf(src)
    if value.Kind() == reflect.Ptr {
        value = value.Elem()
    }
    // [HERE] *** check for valid pointer
    if !value.IsValid() {
        return true
    }
    switch value.Kind() {
    case reflect.String:
        // if src is ptr type and len(string)=0, it returns false
        if !value.IsValid() {
            return true
        }
    case reflect.Int:
        return math.IsNaN(float64(value.Int()))
    case reflect.Float32, reflect.Float64:
        return math.IsNaN(float64(value.Float()))
    }
    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)
    // [HERE] *** unwrap the pointer
    if t.Kind() == reflect.Ptr {
        t = t.Elem()
    }
    return t.Kind() == reflect.Slice || t.Kind() == reflect.Array
}

executor.go

func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*ast.Field, info ResolveInfo, result interface{}) interface{} {
    resultVal := reflect.ValueOf(result)
    // [HERE] *** Unwrap the pointer
    if resultVal.Kind() == reflect.Ptr {
        resultVal = resultVal.Elem()
    }
    parentTypeName := ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant