Skip to content

Commit

Permalink
Remove definition embedding, use normal field instead
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jan 8, 2019
1 parent 950ff42 commit 38add2c
Show file tree
Hide file tree
Showing 24 changed files with 139 additions and 144 deletions.
2 changes: 1 addition & 1 deletion codegen/directive_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (g *Generator) buildDirectives(types NamedTypes) (map[string]*Directive, er
GoVarName: sanitizeArgName(arg.Name),
}

if !newArg.TypeReference.IsInput && !newArg.TypeReference.IsScalar {
if !newArg.TypeReference.Definition.IsInput && !newArg.TypeReference.Definition.IsScalar {
return nil, errors.Errorf("%s cannot be used as argument of directive %s(%s) only input and scalar types are allowed", arg.Type, dir.Name, arg.Name)
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/enum.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package codegen

type Enum struct {
*TypeDefinition
Definition *TypeDefinition
Description string
Values []EnumValue
}
Expand Down
10 changes: 5 additions & 5 deletions codegen/enum_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ func (g *Generator) buildEnums(ts NamedTypes) []Enum {
}

enum := Enum{
TypeDefinition: namedType,
Values: values,
Description: typ.Description,
Definition: namedType,
Values: values,
Description: typ.Description,
}

enum.GoType = types.NewNamed(types.NewTypeName(0, g.Config.Model.Pkg(), templates.ToCamel(enum.GQLType), nil), nil, nil)
enum.Definition.GoType = types.NewNamed(types.NewTypeName(0, g.Config.Model.Pkg(), templates.ToCamel(enum.Definition.GQLType), nil), nil, nil)

enums = append(enums, enum)
}

sort.Slice(enums, func(i, j int) bool {
return enums[i].GQLType < enums[j].GQLType
return enums[i].Definition.GQLType < enums[j].Definition.GQLType
})

return enums
Expand Down
12 changes: 6 additions & 6 deletions codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func (g *Generator) Generate() error {
}

for _, model := range modelsBuild.Models {
modelCfg := g.Models[model.GQLType]
modelCfg.Model = types.TypeString(model.GoType, nil)
g.Models[model.GQLType] = modelCfg
modelCfg := g.Models[model.Definition.GQLType]
modelCfg.Model = types.TypeString(model.Definition.GoType, nil)
g.Models[model.Definition.GQLType] = modelCfg
}

for _, enum := range modelsBuild.Enums {
modelCfg := g.Models[enum.GQLType]
modelCfg.Model = types.TypeString(enum.GoType, nil)
g.Models[enum.GQLType] = modelCfg
modelCfg := g.Models[enum.Definition.GQLType]
modelCfg.Model = types.TypeString(enum.Definition.GoType, nil)
g.Models[enum.Definition.GQLType] = modelCfg
}
}

Expand Down
10 changes: 5 additions & 5 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (g *Generator) buildInputs(namedTypes NamedTypes, prog *loader.Program) (Ob
return nil, err
}

if _, isMap := input.GoType.(*types.Map); !isMap {
if _, isMap := input.Definition.GoType.(*types.Map); !isMap {
bindErrs := bindObject(input, g.StructTag)
if len(bindErrs) > 0 {
return nil, bindErrs
Expand All @@ -33,14 +33,14 @@ func (g *Generator) buildInputs(namedTypes NamedTypes, prog *loader.Program) (Ob
}

sort.Slice(inputs, func(i, j int) bool {
return inputs[i].GQLType < inputs[j].GQLType
return inputs[i].Definition.GQLType < inputs[j].Definition.GQLType
})

return inputs, nil
}

func (g *Generator) buildInput(types NamedTypes, typ *ast.Definition) (*Object, error) {
obj := &Object{TypeDefinition: types[typ.Name]}
obj := &Object{Definition: types[typ.Name]}
typeEntry, entryExists := g.Models[typ.Name]

for _, field := range typ.Fields {
Expand Down Expand Up @@ -69,8 +69,8 @@ func (g *Generator) buildInput(types NamedTypes, typ *ast.Definition) (*Object,
}
}

if !newField.TypeReference.IsInput && !newField.TypeReference.IsScalar {
return nil, errors.Errorf("%s cannot be used as a field of %s. only input and scalar types are allowed", newField.GQLType, obj.GQLType)
if !newField.TypeReference.Definition.IsInput && !newField.TypeReference.Definition.IsScalar {
return nil, errors.Errorf("%s cannot be used as a field of %s. only input and scalar types are allowed", newField.Definition.GQLType, obj.Definition.GQLType)
}

obj.Fields = append(obj.Fields, newField)
Expand Down
6 changes: 2 additions & 4 deletions codegen/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package codegen
import (
"testing"

"github.com/vektah/gqlparser/gqlerror"

"github.com/vektah/gqlparser/ast"

"github.com/99designs/gqlgen/codegen/config"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
"golang.org/x/tools/go/loader"
)

Expand Down
6 changes: 2 additions & 4 deletions codegen/interface.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package codegen

type Interface struct {
*TypeDefinition

Definition *TypeDefinition
Implementors []InterfaceImplementor
}

type InterfaceImplementor struct {
ValueReceiver bool

*TypeDefinition
Definition *TypeDefinition
}
8 changes: 4 additions & 4 deletions codegen/interface_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ func (g *Generator) buildInterfaces(types NamedTypes, prog *loader.Program) []*I
}

sort.Slice(interfaces, func(i, j int) bool {
return interfaces[i].GQLType < interfaces[j].GQLType
return interfaces[i].Definition.GQLType < interfaces[j].Definition.GQLType
})

return interfaces
}

func (g *Generator) buildInterface(types NamedTypes, typ *ast.Definition, prog *loader.Program) *Interface {
i := &Interface{TypeDefinition: types[typ.Name]}
i := &Interface{Definition: types[typ.Name]}

for _, implementor := range g.schema.GetPossibleTypes(typ) {
t := types[implementor.Name]

i.Implementors = append(i.Implementors, InterfaceImplementor{
TypeDefinition: t,
ValueReceiver: g.isValueReceiver(types[typ.Name], t, prog),
Definition: t,
ValueReceiver: g.isValueReceiver(types[typ.Name], t, prog),
})
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/model.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package codegen

type Model struct {
*TypeDefinition
Definition *TypeDefinition
Description string
Fields []ModelField
Implements []*TypeDefinition
Expand Down
12 changes: 6 additions & 6 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ func (g *Generator) buildModels(types NamedTypes, prog *loader.Program) ([]Model
}

sort.Slice(models, func(i, j int) bool {
return models[i].GQLType < models[j].GQLType
return models[i].Definition.GQLType < models[j].Definition.GQLType
})

return models, nil
}

func (g *Generator) obj2Model(obj *Object) Model {
model := Model{
TypeDefinition: obj.TypeDefinition,
Implements: obj.Implements,
Fields: []ModelField{},
Definition: obj.Definition,
Implements: obj.Implements,
Fields: []ModelField{},
}

for i := range obj.Fields {
Expand All @@ -74,8 +74,8 @@ func (g *Generator) obj2Model(obj *Object) Model {

func int2Model(obj *Interface) Model {
model := Model{
TypeDefinition: obj.TypeDefinition,
Fields: []ModelField{},
Definition: obj.Definition,
Fields: []ModelField{},
}

return model
Expand Down
27 changes: 13 additions & 14 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ const (
)

type Object struct {
*TypeDefinition

Definition *TypeDefinition
Fields []Field
Satisfies []string
Implements []*TypeDefinition
Expand Down Expand Up @@ -65,7 +64,7 @@ type FieldArgument struct {
type Objects []*Object

func (o *Object) Implementors() string {
satisfiedBy := strconv.Quote(o.GQLType)
satisfiedBy := strconv.Quote(o.Definition.GQLType)
for _, s := range o.Satisfies {
satisfiedBy += ", " + strconv.Quote(s)
}
Expand Down Expand Up @@ -103,7 +102,7 @@ func (o *Object) IsConcurrent() bool {
}

func (o *Object) IsReserved() bool {
return strings.HasPrefix(o.GQLType, "__")
return strings.HasPrefix(o.Definition.GQLType, "__")
}

func (f *Field) HasDirectives() bool {
Expand Down Expand Up @@ -146,23 +145,23 @@ func (f *Field) ShortInvocation() string {
return ""
}

return fmt.Sprintf("%s().%s(%s)", f.Object.GQLType, f.GoNameExported(), f.CallArgs())
return fmt.Sprintf("%s().%s(%s)", f.Object.Definition.GQLType, f.GoNameExported(), f.CallArgs())
}

func (f *Field) ArgsFunc() string {
if len(f.Args) == 0 {
return ""
}

return "field_" + f.Object.GQLType + "_" + f.GQLName + "_args"
return "field_" + f.Object.Definition.GQLType + "_" + f.GQLName + "_args"
}

func (f *Field) ResolverType() string {
if !f.IsResolver() {
return ""
}

return fmt.Sprintf("%s().%s(%s)", f.Object.GQLType, f.GoNameExported(), f.CallArgs())
return fmt.Sprintf("%s().%s(%s)", f.Object.Definition.GQLType, f.GoNameExported(), f.CallArgs())
}

func (f *Field) ShortResolverDeclaration() string {
Expand All @@ -172,7 +171,7 @@ func (f *Field) ShortResolverDeclaration() string {
res := fmt.Sprintf("%s(ctx context.Context", f.GoNameExported())

if !f.Object.Root {
res += fmt.Sprintf(", obj *%s", templates.CurrentImports.LookupType(f.Object.GoType))
res += fmt.Sprintf(", obj *%s", templates.CurrentImports.LookupType(f.Object.Definition.GoType))
}
for _, arg := range f.Args {
res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature())
Expand All @@ -191,10 +190,10 @@ func (f *Field) ResolverDeclaration() string {
if !f.IsResolver() {
return ""
}
res := fmt.Sprintf("%s_%s(ctx context.Context", f.Object.GQLType, f.GoNameUnexported())
res := fmt.Sprintf("%s_%s(ctx context.Context", f.Object.Definition.GQLType, f.GoNameUnexported())

if !f.Object.Root {
res += fmt.Sprintf(", obj *%s", templates.CurrentImports.LookupType(f.Object.GoType))
res += fmt.Sprintf(", obj *%s", templates.CurrentImports.LookupType(f.Object.Definition.GoType))
}
for _, arg := range f.Args {
res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature())
Expand Down Expand Up @@ -326,12 +325,12 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
"index": index,
"top": depth == 1,
"arrayLen": len(val),
"isScalar": f.IsScalar,
"isScalar": f.Definition.IsScalar,
"usePtr": usePtr,
"next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], astType.Elem, false, depth+1),
})

case f.IsScalar:
case f.Definition.IsScalar:
if isPtr {
val = "*" + val
}
Expand All @@ -343,7 +342,7 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
}
return tpl(`
return ec._{{.type}}(ctx, field.Selections, {{.val}})`, map[string]interface{}{
"type": f.GQLType,
"type": f.Definition.GQLType,
"val": val,
})
}
Expand All @@ -355,7 +354,7 @@ func (f *FieldArgument) Stream() bool {

func (os Objects) ByName(name string) *Object {
for i, o := range os {
if strings.EqualFold(o.GQLType, name) {
if strings.EqualFold(o.Definition.GQLType, name) {
return os[i]
}
}
Expand Down
12 changes: 6 additions & 6 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (g *Generator) buildObjects(ts NamedTypes, prog *loader.Program) (Objects,
return nil, err
}

if _, isMap := obj.GoType.(*types.Map); !isMap {
if _, isMap := obj.Definition.GoType.(*types.Map); !isMap {
for _, bindErr := range bindObject(obj, g.StructTag) {
log.Println(bindErr.Error())
log.Println(" Adding resolver method")
Expand All @@ -36,7 +36,7 @@ func (g *Generator) buildObjects(ts NamedTypes, prog *loader.Program) (Objects,
}

sort.Slice(objects, func(i, j int) bool {
return objects[i].GQLType < objects[j].GQLType
return objects[i].Definition.GQLType < objects[j].Definition.GQLType
})

return objects, nil
Expand Down Expand Up @@ -81,10 +81,10 @@ func sanitizeArgName(name string) string {
}

func (g *Generator) buildObject(ts NamedTypes, typ *ast.Definition) (*Object, error) {
obj := &Object{TypeDefinition: ts[typ.Name]}
obj := &Object{Definition: ts[typ.Name]}
typeEntry, entryExists := g.Models[typ.Name]

tt := types.NewTypeName(0, g.Config.Exec.Pkg(), obj.GQLType+"Resolver", nil)
tt := types.NewTypeName(0, g.Config.Exec.Pkg(), obj.Definition.GQLType+"Resolver", nil)
obj.ResolverInterface = types.NewNamed(tt, nil, nil)

if typ == g.schema.Query {
Expand Down Expand Up @@ -158,8 +158,8 @@ func (g *Generator) buildObject(ts NamedTypes, typ *ast.Definition) (*Object, er
Directives: dirs,
}

if !newArg.TypeReference.IsInput && !newArg.TypeReference.IsScalar {
return nil, errors.Errorf("%s cannot be used as argument of %s.%s. only input and scalar types are allowed", arg.Type, obj.GQLType, field.Name)
if !newArg.TypeReference.Definition.IsInput && !newArg.TypeReference.Definition.IsScalar {
return nil, errors.Errorf("%s cannot be used as argument of %s.%s. only input and scalar types are allowed", arg.Type, obj.Definition.GQLType, field.Name)
}

if arg.DefaultValue != nil {
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/args.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
return nil, err
}
{{- end }}
{{- if $arg.IsInput }}
{{- if $arg.Definition.IsInput }}
{{ $arg.Middleware (print "arg" $i) (print "arg" $i) }}
{{- end }}
}
Expand Down
Loading

0 comments on commit 38add2c

Please sign in to comment.