Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lint-friendly small interfaces option for resolvers #134

Merged
merged 12 commits into from
Jun 26, 2018
31 changes: 31 additions & 0 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,44 @@ func (o *Object) Implementors() string {
return "[]string{" + satisfiedBy + "}"
}

func (o *Object) HasResolvers() bool {
for _, f := range o.Fields {
if f.IsResolver() {
return true
}
}
return false
}

func (f *Field) IsResolver() bool {
return f.GoMethodName == "" && f.GoVarName == ""
}

func (f *Field) IsConcurrent() bool {
return f.IsResolver() && !f.Object.DisableConcurrency
}
func (f *Field) ShortInvocation() string {
if !f.IsResolver() {
return ""
}
shortName := strings.ToUpper(f.GQLName[:1]) + f.GQLName[1:]
res := fmt.Sprintf("%s().%s(ctx", f.Object.GQLType, shortName)
if !f.Object.Root {
res += fmt.Sprintf(", obj")
}
for _, arg := range f.Args {
res += fmt.Sprintf(", %s", arg.GoVarName)
}
res += ")"
return res
}
func (f *Field) ShortResolverDeclaration() string {
if !f.IsResolver() {
return ""
}
decl := strings.TrimPrefix(f.ResolverDeclaration(), f.Object.GQLType+"_")
return strings.ToUpper(decl[:1]) + decl[1:]
}

func (f *Field) ResolverDeclaration() string {
if !f.IsResolver() {
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

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

38 changes: 38 additions & 0 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import (
{{ end }}
)

// MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface.
func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {
return &executableSchema{resolvers: resolvers}
}

// NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.
func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema {
return MakeExecutableSchema(shortMapper{r: resolvers})
}

type Resolvers interface {
{{- range $object := .Objects -}}
{{ range $field := $object.Fields -}}
Expand All @@ -20,6 +26,38 @@ type Resolvers interface {
{{- end }}
}

type ResolverRoot interface {
{{- range $object := .Objects -}}
{{ if $object.HasResolvers -}}
{{$object.GQLType}}() {{$object.GQLType}}Resolver
{{ end }}
{{- end }}
}

{{- range $object := .Objects -}}
{{ if $object.HasResolvers }}
type {{$object.GQLType}}Resolver interface {
{{ range $field := $object.Fields -}}
{{ $field.ShortResolverDeclaration }}
{{ end }}
}
{{- end }}
{{- end }}

type shortMapper struct {
r ResolverRoot
}

{{- range $object := .Objects -}}
{{ range $field := $object.Fields -}}
{{- if $field.IsResolver }}
func (s shortMapper) {{ $field.ResolverDeclaration }} {
return s.r.{{$field.ShortInvocation}}
}
{{- end }}
{{ end }}
{{- end }}

type executableSchema struct {
resolvers Resolvers
}
Expand Down
Loading