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

Handle colliding fields in complexity root gracefully #622

Merged
merged 1 commit into from
Mar 14, 2019
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
11 changes: 11 additions & 0 deletions codegen/complexity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package codegen

func (o *Object) UniqueFields() map[string]*Field {
m := map[string]*Field{}

for _, f := range o.Fields {
m[f.GoFieldName] = f
}

return m
}
1 change: 1 addition & 0 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Data struct {
Inputs Objects
Interfaces map[string]*Interface
ReferencedTypes map[string]*config.TypeReference
ComplexityRoots map[string]*Object

QueryRoot *Object
MutationRoot *Object
Expand Down
4 changes: 2 additions & 2 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ComplexityRoot struct {
{{ range $object := .Objects }}
{{ if not $object.IsReserved -}}
{{ $object.Name|go }} struct {
{{ range $field := $object.Fields -}}
{{ range $field := $object.UniqueFields -}}
{{ if not $field.IsReserved -}}
{{ $field.GoFieldName }} {{ $field.ComplexitySignature }}
{{ end }}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
switch typeName + "." + field {
{{ range $object := .Objects }}
{{ if not $object.IsReserved }}
{{ range $field := $object.Fields }}
{{ range $field := $object.UniqueFields }}
{{ if not $field.IsReserved }}
case "{{$object.Name}}.{{$field.GoFieldName}}":
if e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}} == nil {
Expand Down
11 changes: 11 additions & 0 deletions codegen/testserver/complexity.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extend type Query {
overlapping: OverlappingFields
}

type OverlappingFields {
oneFoo: Int!
twoFoo: Int!
oldFoo: Int!
newFoo: Int!
new_foo: Int!
}
46 changes: 46 additions & 0 deletions codegen/testserver/complexity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package testserver

import (
"context"
"net/http/httptest"
"testing"

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

func TestComplexityCollisions(t *testing.T) {
resolvers := &Stub{}

srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
c := client.New(srv.URL)

resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
return &OverlappingFields{
Foo: 2,
NewFoo: 3,
}, nil
}

resolvers.OverlappingFieldsResolver.OldFoo = func(ctx context.Context, obj *OverlappingFields) (i int, e error) {
return obj.Foo, nil
}

var resp struct {
Overlapping struct {
OneFoo int `json:"oneFoo"`
TwoFoo int `json:"twoFoo"`
OldFoo int `json:"oldFoo"`
NewFoo int `json:"newFoo"`
New_foo int `json:"new_foo"`
}
}
c.MustPost(`query { overlapping { oneFoo, twoFoo, oldFoo, newFoo, new_foo } }`, &resp)
require.Equal(t, 2, resp.Overlapping.OneFoo)
require.Equal(t, 2, resp.Overlapping.TwoFoo)
require.Equal(t, 2, resp.Overlapping.OldFoo)
require.Equal(t, 3, resp.Overlapping.NewFoo)
require.Equal(t, 3, resp.Overlapping.New_foo)

}
Loading