Skip to content

Commit

Permalink
feat: generate resolvers for inputs if fields are missing (#1404)
Browse files Browse the repository at this point in the history
Co-authored-by: daniel vladco <[email protected]>
Co-authored-by: [email protected] <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2021
1 parent 7db941a commit 3bbc2a3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
10 changes: 10 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ func (f *Field) GoNameUnexported() string {
}

func (f *Field) ShortInvocation() string {
if f.Object.Kind == ast.InputObject {
return fmt.Sprintf("%s().%s(ctx, &it, data)", strings.Title(f.Object.Definition.Name), f.GoFieldName)
}
return fmt.Sprintf("%s().%s(%s)", strings.Title(f.Object.Definition.Name), f.GoFieldName, f.CallArgs())
}

Expand All @@ -481,6 +484,13 @@ func (f *Field) ResolverType() string {
}

func (f *Field) ShortResolverDeclaration() string {
if f.Object.Kind == ast.InputObject {
return fmt.Sprintf("(ctx context.Context, obj %s, data %s) error",
templates.CurrentImports.LookupType(f.Object.Reference()),
templates.CurrentImports.LookupType(f.TypeReference.GO),
)
}

res := "(ctx context.Context"

if !f.Object.Root {
Expand Down
19 changes: 18 additions & 1 deletion codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
{{ucFirst $object.Name}}() {{ucFirst $object.Name}}Resolver
{{ end }}
{{- end }}
}
{{- range $object := .Inputs -}}
{{ if $object.HasResolvers -}}
{{ucFirst $object.Name}}() {{ucFirst $object.Name}}Resolver
{{ end }}
{{- end }}
}

type DirectiveRoot struct {
{{ range $directive := .Directives }}
Expand Down Expand Up @@ -72,6 +77,18 @@
{{- end }}
{{- end }}

{{ range $object := .Inputs -}}
{{ if $object.HasResolvers }}
type {{$object.Name}}Resolver interface {
{{ range $field := $object.Fields -}}
{{- if $field.IsResolver }}
{{- $field.GoFieldName}}{{ $field.ShortResolverDeclaration }}
{{- end }}
{{ end }}
}
{{- end }}
{{- end }}

{{ if eq .Config.Exec.Layout "single-file" }}
type executableSchema struct {
resolvers ResolverRoot
Expand Down
28 changes: 23 additions & 5 deletions codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,38 @@
return it, graphql.ErrorOnPath(ctx, err)
}
if data, ok := tmp.({{ $field.TypeReference.GO | ref }}) ; ok {
it.{{$field.GoFieldName}} = data
{{- if $field.IsResolver }}
if err = ec.resolvers.{{ $field.ShortInvocation }}; err != nil {
return it, err
}
{{- else }}
it.{{$field.GoFieldName}} = data
{{- end }}
{{- if $field.TypeReference.IsNilable }}
{{- if not $field.IsResolver }}
} else if tmp == nil {
it.{{$field.GoFieldName}} = nil
{{- end }}
{{- end }}
} else {
err := fmt.Errorf(`unexpected type %T from directive, should be {{ $field.TypeReference.GO }}`, tmp)
return it, graphql.ErrorOnPath(ctx, err)
}
{{- else }}
it.{{$field.GoFieldName}}, err = ec.{{ $field.TypeReference.UnmarshalFunc }}(ctx, v)
if err != nil {
return it, err
}
{{- if $field.IsResolver }}
data, err := ec.{{ $field.TypeReference.UnmarshalFunc }}(ctx, v)
if err != nil {
return it, err
}
if err = ec.resolvers.{{ $field.ShortInvocation }}; err != nil {
return it, err
}
{{- else }}
it.{{$field.GoFieldName}}, err = ec.{{ $field.TypeReference.UnmarshalFunc }}(ctx, v)
if err != nil {
return it, err
}
{{- end }}
{{- end }}
{{- end }}
}
Expand Down

0 comments on commit 3bbc2a3

Please sign in to comment.