Skip to content

Commit

Permalink
Merge pull request #331 from edsrzf/arg-refactor
Browse files Browse the repository at this point in the history
Refactor arg codegen
  • Loading branch information
vektah authored Sep 6, 2018
2 parents 8026e63 + 31505ff commit 72edf98
Show file tree
Hide file tree
Showing 15 changed files with 1,666 additions and 1,719 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
46 changes: 31 additions & 15 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ type DirectiveRoot struct {

type ComplexityRoot struct {
{{ range $object := .Objects }}
{{ if $object.IsReserved }}{{ else -}}
{{ if not $object.IsReserved -}}
{{ $object.GQLType|toCamel }} struct {
{{ range $field := $object.Fields -}}
{{ if $field.IsReserved }}{{ else -}}
{{ if not $field.IsReserved -}}
{{ $field.GQLName|toCamel }} {{ $field.ComplexitySignature }}
{{ end }}
{{- end }}
Expand All @@ -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 All @@ -74,24 +92,18 @@ func (e *executableSchema) Schema() *ast.Schema {
func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
switch typeName + "." + field {
{{ range $object := .Objects }}
{{ if $object.IsReserved }}{{ else }}
{{ if not $object.IsReserved }}
{{ range $field := $object.Fields }}
{{ if $field.IsReserved }}{{ else }}
{{ if not $field.IsReserved }}
case "{{$object.GQLType}}.{{$field.GQLName}}":
if e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}} == nil {
break
}
{{ if $field.Args }}args := map[string]interface{}{} {{end}}
{{ range $i, $arg := $field.Args }}
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 {
return 0, false
}
{{ if $field.Args }}
args, err := {{ $field.ArgsFunc }}(rawArgs)
if err != nil {
return 0, false
}
args[{{$arg.GQLName|quote}}] = arg{{$i}}
{{ end }}
return e.complexity.{{$object.GQLType|toCamel}}.{{$field.GQLName|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
{{ end }}
Expand Down Expand Up @@ -211,7 +223,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
Loading

0 comments on commit 72edf98

Please sign in to comment.