Skip to content

Commit

Permalink
Refactor GoVarName and GoMethodName to GoFieldName etc...
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Aug 1, 2018
1 parent d7e2466 commit 353319c
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 55 deletions.
9 changes: 4 additions & 5 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ func (cfg *Config) buildInput(types NamedTypes, typ *ast.Definition) (*Object, e
var goVarName string
if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
// It may be a method but treat it as a variable now
goVarName = typeField.ModelName
}
}

newField := Field{
GQLName: field.Name,
Type: types.getType(field.Type),
Object: obj,
GoVarName: goVarName,
GQLName: field.Name,
Type: types.getType(field.Type),
Object: obj,
GoFieldName: goVarName,
}

if field.DefaultValue != nil {
Expand Down
8 changes: 4 additions & 4 deletions codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type Model struct {

type ModelField struct {
*Type
GQLName string
GoVarName string
GoFKName string
GoFKType string
GQLName string
GoFieldName string
GoFKName string
GoFKType string
}
10 changes: 5 additions & 5 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func (cfg *Config) obj2Model(obj *Object) Model {
field := &obj.Fields[i]
mf := ModelField{Type: field.Type, GQLName: field.GQLName}

if field.GoVarName != "" {
mf.GoVarName = field.GoVarName
if field.GoFieldName != "" {
mf.GoFieldName = field.GoFieldName
} else {
mf.GoVarName = ucFirst(field.GQLName)
mf.GoFieldName = ucFirst(field.GQLName)
if mf.IsScalar {
if mf.GoVarName == "Id" {
mf.GoVarName = "ID"
if mf.GoFieldName == "Id" {
mf.GoFieldName = "ID"
}
}
}
Expand Down
37 changes: 27 additions & 10 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"unicode"
)

type GoFieldType int

const (
GoFieldUndefined GoFieldType = iota
GoFieldMethod
GoFieldVariable
)

type Object struct {
*NamedType

Expand All @@ -23,14 +31,15 @@ type Object struct {
type Field struct {
*Type

GQLName string // The name of the field in graphql
GoMethodName string // The name of the method in go, if any
GoVarName string // The name of the var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
GoReceiverName string // The name of method & var receiver in go, if any
GoFieldName string // The name of the method or var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
}

type FieldArgument struct {
Expand Down Expand Up @@ -62,7 +71,15 @@ func (o *Object) HasResolvers() bool {
}

func (f *Field) IsResolver() bool {
return f.ForceResolver || f.GoMethodName == "" && f.GoVarName == ""
return f.ForceResolver || f.GoFieldName == ""
}

func (f *Field) IsMethod() bool {
return f.GoFieldType == GoFieldMethod
}

func (f *Field) IsVariable() bool {
return f.GoFieldType == GoFieldVariable
}

func (f *Field) IsConcurrent() bool {
Expand Down Expand Up @@ -120,7 +137,7 @@ func (f *Field) ResolverDeclaration() string {
func (f *Field) CallArgs() string {
var args []string

if f.GoMethodName == "" {
if f.IsResolver() {
args = append(args, "ctx")

if !f.Object.Root {
Expand Down
29 changes: 16 additions & 13 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,24 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
for _, field := range typ.Fields {
if typ == cfg.schema.Query && field.Name == "__type" {
obj.Fields = append(obj.Fields, Field{
Type: &Type{types["__Schema"], []string{modPtr}, nil},
GQLName: "__schema",
NoErr: true,
GoMethodName: "ec.introspectSchema",
Object: obj,
Type: &Type{types["__Schema"], []string{modPtr}, nil},
GQLName: "__schema",
NoErr: true,
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectSchema",
Object: obj,
})
continue
}
if typ == cfg.schema.Query && field.Name == "__schema" {
obj.Fields = append(obj.Fields, Field{
Type: &Type{types["__Type"], []string{modPtr}, nil},
GQLName: "__type",
NoErr: true,
GoMethodName: "ec.introspectType",
Type: &Type{types["__Type"], []string{modPtr}, nil},
GQLName: "__type",
NoErr: true,
GoFieldType: GoFieldMethod,
GoReceiverName: "ec",
GoFieldName: "introspectType",
Args: []FieldArgument{
{GQLName: "name", Type: &Type{types["String"], []string{}, nil}, Object: &Object{}},
},
Expand All @@ -130,11 +134,10 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
}

var forceResolver bool
var goVarName string
var goName string
if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
// It may be a method but treat it as a variable now
goVarName = typeField.ModelName
goName = typeField.ModelName
forceResolver = typeField.Resolver
}
}
Expand Down Expand Up @@ -168,7 +171,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
Type: types.getType(field.Type),
Args: args,
Object: obj,
GoVarName: goVarName,
GoFieldName: goName,
ForceResolver: forceResolver,
})
}
Expand Down

0 comments on commit 353319c

Please sign in to comment.