Skip to content

Commit

Permalink
Stop force resolver from picking up types from matching fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Aug 10, 2018
1 parent ebdcf74 commit 31478cf
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 6 deletions.
2 changes: 1 addition & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (o *Object) HasResolvers() bool {
}

func (f *Field) IsResolver() bool {
return f.ForceResolver || f.GoFieldName == ""
return f.GoFieldName == ""
}

func (f *Field) IsMethod() bool {
Expand Down
60 changes: 60 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions codegen/testserver/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import (
"net/http"
"testing"

"reflect"

"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)

func TestCompiles(t *testing.T) {
http.Handle("/query", handler.GraphQL(NewExecutableSchema(Config{
Resolvers: &Resolver{},
})))
}

func TestForcedResolverFieldIsPointer(t *testing.T) {
field, ok := reflect.TypeOf((*ForcedResolverResolver)(nil)).Elem().MethodByName("Field")
require.True(t, ok)
require.Equal(t, "*testserver.Circle", field.Type.Out(0).String())
}
4 changes: 4 additions & 0 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ models:
model: "github.com/99designs/gqlgen/codegen/testserver.Circle"
Rectangle:
model: "github.com/99designs/gqlgen/codegen/testserver.Rectangle"
ForcedResolver:
model: "github.com/99designs/gqlgen/codegen/testserver.ForcedResolver"
fields:
field: { resolver: true }
5 changes: 5 additions & 0 deletions codegen/testserver/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package testserver

type ForcedResolver struct {
Field Circle
}
9 changes: 9 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import (

type Resolver struct{}

func (r *Resolver) ForcedResolver() ForcedResolverResolver {
return &forcedResolverResolver{r}
}
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}

type forcedResolverResolver struct{ *Resolver }

func (r *forcedResolverResolver) Field(ctx context.Context, obj *ForcedResolver) (*Circle, error) {
panic("not implemented")
}

type queryResolver struct{ *Resolver }

func (r *queryResolver) InvalidIdentifier(ctx context.Context) (*invalid_packagename.InvalidIdentifier, error) {
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ type Rectangle implements Shape {
area: Float
}
union ShapeUnion = Circle | Rectangle

type ForcedResolver {
field: Circle
}
8 changes: 7 additions & 1 deletion codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func bindObject(t types.Type, object *Object, imports *Imports) BindErrors {
for i := range object.Fields {
field := &object.Fields[i]

if field.ForceResolver {
continue
}

// first try binding to a method
methodErr := bindMethod(imports, t, field)
if methodErr == nil {
Expand Down Expand Up @@ -261,7 +265,9 @@ nextArg:
param := params.At(j)
for _, oldArg := range field.Args {
if strings.EqualFold(oldArg.GQLName, param.Name()) {
oldArg.Type.Modifiers = modifiersFromGoType(param.Type())
if !field.ForceResolver {
oldArg.Type.Modifiers = modifiersFromGoType(param.Type())
}
newArgs = append(newArgs, oldArg)
continue nextArg
}
Expand Down
8 changes: 4 additions & 4 deletions example/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ func TestSkipAndIncludeDirectives(t *testing.T) {
Expected bool
}
table := []TestCase{
TestCase{Skip: true, Include: true, Expected: false},
TestCase{Skip: true, Include: false, Expected: false},
TestCase{Skip: false, Include: true, Expected: true},
TestCase{Skip: false, Include: false, Expected: false},
{Skip: true, Include: true, Expected: false},
{Skip: true, Include: false, Expected: false},
{Skip: false, Include: true, Expected: true},
{Skip: false, Include: false, Expected: false},
}
q := `query Test($skip: Boolean!, $include: Boolean!) { todo(id: 1) @skip(if: $skip) @include(if: $include) { __typename } }`
for _, tc := range table {
Expand Down

0 comments on commit 31478cf

Please sign in to comment.