From 555d7468922e2a27411e688f783dcda5c450554c Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Tue, 5 Feb 2019 11:13:22 +1100 Subject: [PATCH] Remove TypeDefinition from interface building --- codegen/data.go | 10 +++------- codegen/interface.go | 33 +++++++++++++++++++++++---------- codegen/interface.gotpl | 10 +++++----- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/codegen/data.go b/codegen/data.go index bd3697e5492..1d0c6660da5 100644 --- a/codegen/data.go +++ b/codegen/data.go @@ -19,7 +19,7 @@ type Data struct { Directives map[string]*Directive Objects Objects Inputs Objects - Interfaces []*Interface + Interfaces map[string]*Interface QueryRoot *Object MutationRoot *Object @@ -77,6 +77,7 @@ func BuildData(cfg *config.Config) (*Data, error) { Directives: b.Directives, Schema: b.Schema, SchemaStr: b.SchemaStr, + Interfaces: map[string]*Interface{}, } for _, schemaType := range b.Schema.Types { @@ -97,8 +98,7 @@ func BuildData(cfg *config.Config) (*Data, error) { s.Inputs = append(s.Inputs, input) case ast.Union, ast.Interface: - s.Interfaces = append(s.Interfaces, b.buildInterface(schemaType)) - + s.Interfaces[schemaType.Name] = b.buildInterface(schemaType) } } @@ -128,10 +128,6 @@ func BuildData(cfg *config.Config) (*Data, error) { return s.Inputs[i].Definition.Name < s.Inputs[j].Definition.Name }) - sort.Slice(s.Interfaces, func(i, j int) bool { - return s.Interfaces[i].Definition.GQLDefinition.Name < s.Interfaces[j].Definition.GQLDefinition.Name - }) - return &s, nil } diff --git a/codegen/interface.go b/codegen/interface.go index 1c7a3f1ccad..ee67592508c 100644 --- a/codegen/interface.go +++ b/codegen/interface.go @@ -7,41 +7,54 @@ import ( ) type Interface struct { - Definition *TypeDefinition + *ast.Definition + Type types.Type Implementors []InterfaceImplementor InTypemap bool } type InterfaceImplementor struct { - ValueReceiver bool - Definition *TypeDefinition + *ast.Definition + + Interface *Interface + Type types.Type } func (b *builder) buildInterface(typ *ast.Definition) *Interface { + obj, err := b.Binder.FindUserObject(typ.Name) + if err != nil { + panic(err) + } + i := &Interface{ - Definition: b.NamedTypes[typ.Name], + Definition: typ, + Type: obj, InTypemap: b.Config.Models.UserDefined(typ.Name), } for _, implementor := range b.Schema.GetPossibleTypes(typ) { - t := b.NamedTypes[implementor.Name] + obj, err := b.Binder.FindUserObject(implementor.Name) + if err != nil { + panic(err) + } i.Implementors = append(i.Implementors, InterfaceImplementor{ - Definition: t, - ValueReceiver: b.isValueReceiver(b.NamedTypes[typ.Name], t), + Definition: implementor, + Type: obj, + Interface: i, }) } return i } -func (b *builder) isValueReceiver(intf *TypeDefinition, implementor *TypeDefinition) bool { - interfaceType, err := findGoInterface(intf.GoType) +func (i *InterfaceImplementor) ValueReceiver() bool { + interfaceType, err := findGoInterface(i.Interface.Type) if interfaceType == nil || err != nil { return true } - implementorType, err := findGoNamedType(implementor.GoType) + implementorType, err := findGoNamedType(i.Type) if implementorType == nil || err != nil { return true } diff --git a/codegen/interface.gotpl b/codegen/interface.gotpl index 57d6c6f06ed..81a5807653c 100644 --- a/codegen/interface.gotpl +++ b/codegen/interface.gotpl @@ -1,16 +1,16 @@ {{- range $interface := .Interfaces }} -func (ec *executionContext) _{{$interface.Definition.GQLDefinition.Name}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.Definition.GoType | ref}}) graphql.Marshaler { +func (ec *executionContext) _{{$interface.Name}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.Type | ref}}) graphql.Marshaler { switch obj := (*obj).(type) { case nil: return graphql.Null {{- range $implementor := $interface.Implementors }} {{- if $implementor.ValueReceiver }} - case {{$implementor.Definition.GoType | ref}}: - return ec._{{$implementor.Definition.GQLDefinition.Name}}(ctx, sel, &obj) + case {{$implementor.Type | ref}}: + return ec._{{$implementor.Name}}(ctx, sel, &obj) {{- end}} - case *{{$implementor.Definition.GoType | ref}}: - return ec._{{$implementor.Definition.GQLDefinition.Name}}(ctx, sel, obj) + case *{{$implementor.Type | ref}}: + return ec._{{$implementor.Name}}(ctx, sel, obj) {{- end }} default: panic(fmt.Errorf("unexpected type %T", obj))