Skip to content

Commit

Permalink
Removed named types
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 5, 2019
1 parent 533b08b commit cf94d3b
Show file tree
Hide file tree
Showing 28 changed files with 5,791 additions and 7,277 deletions.
8 changes: 4 additions & 4 deletions codegen/args.gotpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{{ range $name, $args := .Args }}
func (e *executableSchema) {{ $name }}(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
func (ec *executionContext) {{ $name }}(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
var err error
args := map[string]interface{}{}
{{- range $i, $arg := . }}
var arg{{$i}} {{ $arg.TypeReference.GO | ref}}
if tmp, ok := rawArgs[{{$arg.Name|quote}}]; ok {
{{- if $arg.Directives }}
getArg0 := func(ctx context.Context) (interface{}, error) { return e.unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp) }
getArg0 := func(ctx context.Context) (interface{}, error) { return ec.unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp) }

{{- range $i, $directive := $arg.Directives }}
getArg{{add $i 1}} := func(ctx context.Context) (res interface{}, err error) {
Expand All @@ -16,7 +16,7 @@ func (e *executableSchema) {{ $name }}(ctx context.Context, rawArgs map[string]i
{{- end }}
{{- end }}
n := getArg{{$i}}
return e.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs "tmp" "n" }})
return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs "tmp" "n" }})
}
{{- end }}

Expand All @@ -30,7 +30,7 @@ func (e *executableSchema) {{ $name }}(ctx context.Context, rawArgs map[string]i
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ $arg.TypeReference.GO }}`, tmp)
}
{{- else }}
arg{{$i}}, err = e.unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp)
arg{{$i}}, err = ec.unmarshal{{$arg.TypeReference.GQL.Name}}2{{ $arg.TypeReference.GO | ts }}(tmp)
if err != nil {
return nil, err
}
Expand Down
116 changes: 0 additions & 116 deletions codegen/build_typedef.go

This file was deleted.

41 changes: 39 additions & 2 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,43 @@ func (t TypeReference) IsNamed() bool {
return isSlice
}

func (t TypeReference) IsScalar() bool {
return t.Definition.Kind == ast.Scalar
}

func (t TypeReference) SelfMarshalling() bool {
it := t.GO
if ptr, isPtr := it.(*types.Pointer); isPtr {
it = ptr.Elem()
}
namedType, ok := it.(*types.Named)
if !ok {
return false
}

for i := 0; i < namedType.NumMethods(); i++ {
switch namedType.Method(i).Name() {
case "MarshalGQL":
return true
}
}
return false
}

func (t TypeReference) NeedsUnmarshaler() bool {
if t.Definition == nil {
panic(errors.New("Definition missing for " + t.GQL.Name()))
}
return t.Definition.IsInputType()
}

func (t TypeReference) NeedsMarshaler() bool {
if t.Definition == nil {
panic(errors.New("Definition missing for " + t.GQL.Name()))
}
return t.Definition.Kind != ast.InputObject
}

func (b *Binder) PushRef(ret *TypeReference) {
b.References = append(b.References, ret)
}
Expand Down Expand Up @@ -214,11 +251,11 @@ func (b *Binder) TypeReference(schemaType *ast.Type) (ret *TypeReference, err er

// Special case to reference generated unmarshal functions
if !hasUnmarshal {
ref.Unmarshaler = types.NewFunc(0, b.cfg.Exec.Pkg(), "e.unmarshalInput"+schemaType.Name(), nil)
ref.Unmarshaler = types.NewFunc(0, b.cfg.Exec.Pkg(), "ec.unmarshalInput"+schemaType.Name(), nil)
}
}

ref.GO = b.CopyModifiersFromAst(schemaType, true, ref.GO)
ref.GO = b.CopyModifiersFromAst(schemaType, def.Kind != ast.Interface, ref.GO)

return ref, nil
}
Expand Down
6 changes: 3 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func (tm TypeMap) ReferencedPackages() []string {
return pkgs
}

func (tm TypeMap) Add(gqlName string, goType string) {
modelCfg := tm[gqlName]
func (tm TypeMap) Add(Name string, goType string) {
modelCfg := tm[Name]
modelCfg.Model = goType
tm[gqlName] = modelCfg
tm[Name] = modelCfg
}

func inStrSlice(haystack []string, needle string) bool {
Expand Down
27 changes: 10 additions & 17 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package codegen

import (
"fmt"
"go/types"
"sort"

"github.com/99designs/gqlgen/codegen/config"
Expand Down Expand Up @@ -33,7 +32,6 @@ type builder struct {
SchemaStr map[string]string
Binder *config.Binder
Directives map[string]*Directive
NamedTypes NamedTypes
}

func BuildData(cfg *config.Config) (*Data, error) {
Expand All @@ -59,15 +57,6 @@ func BuildData(cfg *config.Config) (*Data, error) {
return nil, err
}

b.NamedTypes = NamedTypes{}

for _, schemaType := range b.Schema.Types {
b.NamedTypes[schemaType.Name], err = b.buildTypeDef(schemaType)
if err != nil {
return nil, errors.Wrap(err, "unable to build type definition")
}
}

b.Directives, err = b.buildDirectives()
if err != nil {
return nil, err
Expand Down Expand Up @@ -143,7 +132,7 @@ func (b *builder) injectIntrospectionRoots(s *Data) error {
return fmt.Errorf("root query type must be defined")
}

typeType, err := b.Binder.FindObject("github.com/99designs/gqlgen/graphql/introspection", "Type")
typeType, err := b.Binder.TypeReference(ast.NamedType("__Type", nil))
if err != nil {
return errors.Wrap(err, "unable to find root Type introspection type")
}
Expand All @@ -153,8 +142,10 @@ func (b *builder) injectIntrospectionRoots(s *Data) error {
}

obj.Fields = append(obj.Fields, &Field{
TypeReference: &TypeReference{b.NamedTypes["__Type"], types.NewPointer(typeType.Type()), ast.NamedType("__Schema", nil)},
GQLName: "__type",
TypeReference: typeType,
FieldDefinition: &ast.FieldDefinition{
Name: "__type",
},
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectType",
Expand All @@ -170,14 +161,16 @@ func (b *builder) injectIntrospectionRoots(s *Data) error {
Object: obj,
})

schemaType, err := b.Binder.FindObject("github.com/99designs/gqlgen/graphql/introspection", "Schema")
schemaType, err := b.Binder.TypeReference(ast.NamedType("__Schema", nil))
if err != nil {
return errors.Wrap(err, "unable to find root Schema introspection type")
}

obj.Fields = append(obj.Fields, &Field{
TypeReference: &TypeReference{b.NamedTypes["__Schema"], types.NewPointer(schemaType.Type()), ast.NamedType("__Schema", nil)},
GQLName: "__schema",
TypeReference: schemaType,
FieldDefinition: &ast.FieldDefinition{
Name: "__schema",
},
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectSchema",
Expand Down
Loading

0 comments on commit cf94d3b

Please sign in to comment.