Skip to content

Commit

Permalink
graphql: add FieldContext.ChildArgs field and enable it in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Mar 29, 2022
1 parent 36fb3dc commit bf9caea
Show file tree
Hide file tree
Showing 52 changed files with 30,364 additions and 11,835 deletions.
1,689 changes: 1,232 additions & 457 deletions _examples/chat/generated.go

Large diffs are not rendered by default.

1,709 changes: 1,247 additions & 462 deletions _examples/config/generated.go

Large diffs are not rendered by default.

1,839 changes: 1,340 additions & 499 deletions _examples/dataloader/generated.go

Large diffs are not rendered by default.

1,761 changes: 1,281 additions & 480 deletions _examples/federation/accounts/graph/generated/generated.go

Large diffs are not rendered by default.

1,869 changes: 1,356 additions & 513 deletions _examples/federation/products/graph/generated/generated.go

Large diffs are not rendered by default.

1,926 changes: 1,400 additions & 526 deletions _examples/federation/reviews/graph/generated/generated.go

Large diffs are not rendered by default.

1,703 changes: 1,243 additions & 460 deletions _examples/fileupload/generated.go

Large diffs are not rendered by default.

1,910 changes: 1,395 additions & 515 deletions _examples/scalars/generated.go

Large diffs are not rendered by default.

1,596 changes: 1,161 additions & 435 deletions _examples/selection/generated.go

Large diffs are not rendered by default.

2,689 changes: 1,913 additions & 776 deletions _examples/starwars/generated/exec.go

Large diffs are not rendered by default.

1,659 changes: 1,214 additions & 445 deletions _examples/todo/generated.go

Large diffs are not rendered by default.

1,606 changes: 1,176 additions & 430 deletions _examples/type-system-extension/generated.go

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ func (f *Field) ArgsFunc() string {
return "field_" + f.Object.Definition.Name + "_" + f.Name + "_args"
}

func (f *Field) FieldContextFunc() string {
return "fieldContext_" + f.Object.Definition.Name + "_" + f.Name
}

func (f *Field) ChildFieldContextFunc(name string) string {
return "fieldContext_" + f.TypeReference.Definition.Name + "_" + name
}

func (f *Field) ResolverType() string {
if !f.IsResolver {
return ""
Expand Down Expand Up @@ -549,7 +557,7 @@ func (f *Field) CallArgs() string {
}

for _, arg := range f.Args {
args = append(args, "args["+strconv.Quote(arg.Name)+"].("+templates.CurrentImports.LookupType(arg.TypeReference.GO)+")")
args = append(args, "fc.Args["+strconv.Quote(arg.Name)+"].("+templates.CurrentImports.LookupType(arg.TypeReference.GO)+")")
}

return strings.Join(args, ", ")
Expand Down
61 changes: 43 additions & 18 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,17 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
{{- if $object.Stream }}
{{- $null = "nil" }}
{{- end }}
fc, err := ec.{{ $field.FieldContextFunc }}(ctx, field)
if err != nil {
return {{ $null }}
}
ctx = graphql.WithFieldContext(ctx, fc)
defer func () {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = {{ $null }}
}
}()
fc := &graphql.FieldContext{
Object: {{$object.Name|quote}},
Field: field,
Args: nil,
IsMethod: {{or $field.IsMethod $field.IsResolver}},
IsResolver: {{ $field.IsResolver }},
}

ctx = graphql.WithFieldContext(ctx, fc)
{{- if $field.Args }}
rawArgs := field.ArgumentMap(ec.Variables)
args, err := ec.{{ $field.ArgsFunc }}(ctx,rawArgs)
if err != nil {
ec.Error(ctx, err)
return {{ $null }}
}
fc.Args = args
{{- end }}
{{- if $.AllDirectives.LocationDirectives "FIELD" }}
resTmp := ec._fieldMiddleware(ctx, {{if $object.Root}}nil{{else}}obj{{end}}, func(rctx context.Context) (interface{}, error) {
{{ template "field" $field }}
Expand Down Expand Up @@ -71,6 +58,44 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
{{- end }}
}

func (ec *executionContext) {{ $field.FieldContextFunc }}(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: {{quote $field.Object.Name}},
Field: field,
IsMethod: {{or $field.IsMethod $field.IsResolver}},
IsResolver: {{ $field.IsResolver }},
Child: func (ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
{{- if not $field.TypeReference.Definition.Fields }}
return nil, errors.New("field of type {{ $field.TypeReference.Definition.Name }} does not have child fields")
{{- else if ne $field.TypeReference.Definition.Kind "OBJECT" }}
return nil, errors.New("FieldContext.Child cannot be called on type {{ $field.TypeReference.Definition.Kind }}")
{{- else }}
switch field.Name {
{{- range $f := $field.TypeReference.Definition.Fields }}
case "{{ $f.Name }}":
return ec.{{ $field.ChildFieldContextFunc $f.Name }}(ctx, field)
{{- end }}
}
return nil, fmt.Errorf("no field named %q was found under type {{ $field.TypeReference.Definition.Name }}", field.Name)
{{- end }}
},
}
{{- if $field.Args }}
defer func () {
if r := recover(); r != nil {
err = ec.Recover(ctx, r)
ec.Error(ctx, err)
}
}()
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.{{ $field.ArgsFunc }}(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return
}
{{- end }}
return fc, nil
}

{{- end }}{{- end}}

{{ define "field" }}
Expand Down
28 changes: 19 additions & 9 deletions codegen/testserver/followschema/builtinscalar.generated.go

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

136 changes: 91 additions & 45 deletions codegen/testserver/followschema/complexity.generated.go

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

Loading

0 comments on commit bf9caea

Please sign in to comment.