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

Properly handle nil interface resolvers #239

Merged
merged 4 commits into from
Oct 8, 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
59 changes: 59 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package graphql_test

import (
"context"
"errors"
"testing"
"time"

"github.com/graph-gophers/graphql-go"
gqlerrors "github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/example/starwars"
"github.com/graph-gophers/graphql-go/gqltesting"
)
Expand Down Expand Up @@ -245,6 +247,63 @@ func TestBasic(t *testing.T) {
})
}

type testNilInterfaceResolver struct{}

func (r *testNilInterfaceResolver) A() interface{ Z() int32 } {
pavelnikolov marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func (r *testNilInterfaceResolver) B() (interface{ Z() int32 }, error) {
return nil, errors.New("x")
}

func (r *testNilInterfaceResolver) C() (interface{ Z() int32 }, error) {
return nil, nil
}

func TestNilInterface(t *testing.T) {
gqltesting.RunTests(t, []*gqltesting.Test{
{
Schema: graphql.MustParseSchema(`
schema {
query: Query
}

type Query {
a: T
b: T
c: T
}

type T {
z: Int!
}
`, &testNilInterfaceResolver{}),
Query: `
{
a { z }
b { z }
c { z }
}
`,
ExpectedResult: `
{
"a": null,
"b": null,
"c": null
}
`,
ExpectedErrors: []*gqlerrors.QueryError{
&gqlerrors.QueryError{
Message: "x",
Path: []interface{}{"b"},
ResolverError: errors.New("x"),
},
},
},
})
}

func TestArguments(t *testing.T) {
gqltesting.RunTests(t, []*gqltesting.Test{
{
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ func (r *Request) execSelectionSet(ctx context.Context, sels []selected.Selectio
t, nonNull := unwrapNonNull(typ)
switch t := t.(type) {
case *schema.Object, *schema.Interface, *schema.Union:
if resolver.Kind() == reflect.Ptr && resolver.IsNil() {
// a reflect.Value of a nil interface will show up as an Invalid value
if resolver.Kind() == reflect.Invalid || ((resolver.Kind() == reflect.Ptr || resolver.Kind() == reflect.Interface) && resolver.IsNil()) {
pavelnikolov marked this conversation as resolved.
Show resolved Hide resolved
if nonNull {
panic(errors.Errorf("got nil for non-null %q", t))
}
Expand Down