Skip to content

Commit

Permalink
Merge pull request #134 from mastercactapus/small-interfaces
Browse files Browse the repository at this point in the history
add lint-friendly small interfaces option for resolvers
  • Loading branch information
vektah authored Jun 26, 2018
2 parents e8c30ac + 769a97e commit 9fa3f0f
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 22 deletions.
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

0 comments on commit 9fa3f0f

Please sign in to comment.