Skip to content

Commit

Permalink
Add method support
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Aug 1, 2018
1 parent 108bb6b commit d7e2466
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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
}
}
Expand Down
1 change: 1 addition & 0 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
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
forceResolver = typeField.Resolver
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
{{- if $field.IsResolver }}
return ec.resolvers.{{ $field.ShortInvocation }}
{{- else if $field.GoVarName }}
return obj.{{$field.GoVarName}}, nil
{{- else if $field.GoMethodName }}
{{- if $field.NoErr }}
return {{$field.GoMethodName}}({{ $field.CallArgs }}), nil
{{- else }}
return {{$field.GoMethodName}}({{ $field.CallArgs }})
{{- end }}
{{- else if $field.GoVarName }}
return obj.{{$field.GoVarName}}, nil
{{- end }}
})
if resTmp == nil {
Expand Down
13 changes: 11 additions & 2 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ func bindMethod(imports *Imports, t types.Type, field *Field) error {
return fmt.Errorf("not a named type")
}

method := findMethod(namedType, field.GQLName)
goName := field.GQLName
if field.GoVarName != "" {
goName = field.GoVarName
}
method := findMethod(namedType, goName)
if method == nil {
return fmt.Errorf("no method named %s", field.GQLName)
}
field.GoVarName = "" // found it to be a method, not a variable
sig := method.Type().(*types.Signature)

if sig.Results().Len() == 1 {
Expand Down Expand Up @@ -227,7 +232,11 @@ func bindVar(imports *Imports, t types.Type, field *Field) error {
return fmt.Errorf("not a struct")
}

structField := findField(underlying, field.GQLName)
goName := field.GQLName
if field.GoVarName != "" {
goName = field.GoVarName
}
structField := findField(underlying, goName)
if structField == nil {
return fmt.Errorf("no field named %s", field.GQLName)
}
Expand Down
6 changes: 3 additions & 3 deletions example/config/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ models:
Todo: # Object
fields:
text:
modelField: Description
modelField: Description # Field
NewTodo: # Input
fields:
userId:
modelField: UserID
modelField: UserID # Field
User:
model: github.com/vektah/gqlgen/example/config.User
fields:
name:
modelField: FullName
modelField: FullName # Method
5 changes: 3 additions & 2 deletions example/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions example/config/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package config

import "fmt"

type User struct {
ID string
FullName string
ID string
FirstName, LastName string
}

func (user *User) FullName() string {
return fmt.Sprintf("%s %s", user.FirstName, user.LastName)
}
2 changes: 1 addition & 1 deletion example/config/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ input NewTodo {

type Mutation {
createTodo(input: NewTodo!): Todo!
}
}

0 comments on commit d7e2466

Please sign in to comment.