Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model field mapping #237

Merged
merged 6 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ type TypeMapEntry struct {
}

type TypeMapField struct {
Resolver bool `yaml:"resolver"`
Resolver bool `yaml:"resolver"`
FieldName string `yaml:"fieldName"`
}

func (c *PackageConfig) normalize() error {
Expand Down
11 changes: 9 additions & 2 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, impo
for _, typ := range cfg.schema.Types {
switch typ.Kind {
case ast.InputObject:
input, err := buildInput(namedTypes, typ)
input, err := cfg.buildInput(namedTypes, typ)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -44,8 +44,9 @@ func (cfg *Config) buildInputs(namedTypes NamedTypes, prog *loader.Program, impo
return inputs, nil
}

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

for _, field := range typ.Fields {
newField := Field{
Expand All @@ -54,6 +55,12 @@ func buildInput(types NamedTypes, typ *ast.Definition) (*Object, error) {
Object: obj,
}

if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
newField.GoFieldName = typeField.FieldName
}
}

if field.DefaultValue != nil {
var err error
newField.Default, err = field.DefaultValue.Value(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
}
14 changes: 9 additions & 5 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program, imports *
}
model = cfg.obj2Model(obj)
case ast.InputObject:
obj, err := buildInput(types, typ)
obj, err := cfg.buildInput(types, typ)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -65,10 +65,14 @@ func (cfg *Config) obj2Model(obj *Object) Model {
field := &obj.Fields[i]
mf := ModelField{Type: field.Type, GQLName: field.GQLName}

mf.GoVarName = ucFirst(field.GQLName)
if mf.IsScalar {
if mf.GoVarName == "Id" {
mf.GoVarName = "ID"
if field.GoFieldName != "" {
mf.GoFieldName = field.GoFieldName
} else {
mf.GoFieldName = ucFirst(field.GQLName)
if mf.IsScalar {
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
25 changes: 16 additions & 9 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,8 +134,10 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
}

var forceResolver bool
var goName string
if entryExists {
if typeField, ok := typeEntry.Fields[field.Name]; ok {
goName = typeField.FieldName
forceResolver = typeField.Resolver
}
}
Expand Down Expand Up @@ -165,6 +171,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
Type: types.getType(field.Type),
Args: args,
Object: obj,
GoFieldName: goName,
ForceResolver: forceResolver,
})
}
Expand Down
Loading