Skip to content

Commit

Permalink
Remove TypeDefinition from interface building
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 5, 2019
1 parent 162afad commit 555d746
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
10 changes: 3 additions & 7 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
}

Expand Down
33 changes: 23 additions & 10 deletions codegen/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions codegen/interface.gotpl
Original file line number Diff line number Diff line change
@@ -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))
Expand Down

0 comments on commit 555d746

Please sign in to comment.