Skip to content

Commit

Permalink
Refactor arg codegen
Browse files Browse the repository at this point in the history
Now a function is generated for each field and directive that has
arguments. This function can be used by both field methods as well as
the `Complexity` method.

The `args.gotpl` template now generates the code for this function, so
its purpose is a little different than it used to be.
  • Loading branch information
edsrzf committed Sep 3, 2018
1 parent 8026e63 commit 639727b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
8 changes: 8 additions & 0 deletions codegen/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ type Directive struct {
Args []FieldArgument
}

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

return "dir_" + d.Name + "_args"
}

func (d *Directive) CallArgs() string {
args := []string{"ctx", "obj", "n"}

Expand Down
14 changes: 11 additions & 3 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ func (f *Field) ShortInvocation() string {
return fmt.Sprintf("%s().%s(%s)", f.Object.GQLType, f.GoNameExported(), f.CallArgs())
}

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

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

func (f *Field) ResolverType() string {
if !f.IsResolver() {
return ""
Expand Down Expand Up @@ -247,7 +255,7 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
{{.arr}} := make(graphql.Array, len({{.val}}))
{{ if and .top (not .isScalar) }} var wg sync.WaitGroup {{ end }}
{{ if not .isScalar }}
isLen1 := len({{.val}}) == 1
isLen1 := len({{.val}}) == 1
if !isLen1 {
wg.Add(len({{.val}}))
}
Expand All @@ -265,7 +273,7 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
defer wg.Done()
}
{{.arr}}[{{.index}}] = func() graphql.Marshaler {
{{ .next }}
{{ .next }}
}()
}
if isLen1 {
Expand All @@ -275,7 +283,7 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
}
{{ else }}
{{.arr}}[{{.index}}] = func() graphql.Marshaler {
{{ .next }}
{{ .next }}
}()
{{- end}}
}
Expand Down
12 changes: 4 additions & 8 deletions codegen/templates/args.gotpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
{{- if . }}args := map[string]interface{}{} {{end}}
args := map[string]interface{}{}
{{- range $i, $arg := . }}
var arg{{$i}} {{$arg.Signature }}
if tmp, ok := rawArgs[{{$arg.GQLName|quote}}]; ok {
var err error
{{$arg.Unmarshal (print "arg" $i) "tmp" }}
if err != nil {
ec.Error(ctx, err)
{{- if $arg.Stream }}
return nil
{{- else }}
return graphql.Null
{{- end }}
return nil, err
}
}
args[{{$arg.GQLName|quote}}] = arg{{$i}}
{{- end -}}
{{- end }}
return args, nil
6 changes: 3 additions & 3 deletions codegen/templates/data.go

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField) func() graphql.Marshaler {
{{- if $field.Args }}
rawArgs := field.ArgumentMap(ec.Variables)
{{ template "args.gotpl" $field.Args }}
args, err := {{ $field.ArgsFunc }}(rawArgs)
if err != nil {
ec.Error(ctx, err)
return nil
}
{{- end }}
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
Field: field,
Expand All @@ -30,7 +34,11 @@
func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler {
{{- if $field.Args }}
rawArgs := field.ArgumentMap(ec.Variables)
{{ template "args.gotpl" $field.Args }}
args, err := {{ $field.ArgsFunc }}(rawArgs)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
{{- end }}
rctx := &graphql.ResolverContext{
Object: {{$object.GQLType|quote}},
Expand Down
24 changes: 23 additions & 1 deletion codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ type ComplexityRoot struct {
{{- end }}
{{- end }}

{{ range $object := .Objects -}}
{{ range $field := $object.Fields -}}
{{ if $field.Args }}
func {{ $field.ArgsFunc }}(rawArgs map[string]interface{}) (map[string]interface{}, error) {
{{ template "args.gotpl" $field.Args }}
}
{{ end }}
{{ end }}
{{- end }}

{{ range $directive := .Directives }}
{{ if $directive.Args }}
func {{ $directive.ArgsFunc }}(rawArgs map[string]interface{}) (map[string]interface{}, error) {
{{ template "args.gotpl" $directive.Args }}
}
{{ end }}
{{ end }}

type executableSchema struct {
resolvers ResolverRoot
directives DirectiveRoot
Expand Down Expand Up @@ -211,7 +229,11 @@ func (ec *executionContext) FieldMiddleware(ctx context.Context, obj interface{}
if ec.directives.{{$directive.Name|ucFirst}} != nil {
{{- if $directive.Args }}
rawArgs := d.ArgumentMap(ec.Variables)
{{ template "args.gotpl" $directive.Args }}
args, err := {{ $directive.ArgsFunc }}(rawArgs)
if err != nil {
ec.Error(ctx, err)
return nil
}
{{- end }}
n := next
next = func(ctx context.Context) (interface{}, error) {
Expand Down

0 comments on commit 639727b

Please sign in to comment.