Skip to content

Commit

Permalink
Merge pull request #622 from 99designs/handle-complexity-root-collisions
Browse files Browse the repository at this point in the history
Handle colliding fields in complexity root gracefully
  • Loading branch information
vektah authored Mar 14, 2019
2 parents 26c395b + c889b31 commit 58744de
Show file tree
Hide file tree
Showing 19 changed files with 867 additions and 470 deletions.
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

0 comments on commit 58744de

Please sign in to comment.