Skip to content

Commit

Permalink
feat: generate resolvers for inputs if fields are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel vladco committed Nov 30, 2020
1 parent 0e12bfb commit d4b43ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
10 changes: 10 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,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)", f.Object.Definition.Name, f.GoFieldName)
}
return fmt.Sprintf("%s().%s(%s)", f.Object.Definition.Name, f.GoFieldName, f.CallArgs())
}

Expand All @@ -472,6 +475,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
17 changes: 17 additions & 0 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type ResolverRoot interface {
{{$object.Name}}() {{$object.Name}}Resolver
{{ end }}
{{- end }}
{{- range $object := .Inputs -}}
{{ if $object.HasResolvers -}}
{{$object.Name}}() {{$object.Name}}Resolver
{{ end }}
{{- end }}
}

type DirectiveRoot struct {
Expand Down Expand Up @@ -70,6 +75,18 @@ type ComplexityRoot struct {
{{- 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 }}

type executableSchema struct {
resolvers ResolverRoot
directives DirectiveRoot
Expand Down
28 changes: 23 additions & 5 deletions codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,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 d4b43ad

Please sign in to comment.