Skip to content

Commit

Permalink
add stub generation plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 7, 2019
1 parent 43db679 commit c5e3dd4
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
5 changes: 1 addition & 4 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,7 @@ func (f *Field) ResolverType() string {
}

func (f *Field) ShortResolverDeclaration() string {
if !f.IsResolver {
return ""
}
res := fmt.Sprintf("%s(ctx context.Context", f.GoFieldName)
res := "(ctx context.Context"

if !f.Object.Root {
res += fmt.Sprintf(", obj *%s", templates.CurrentImports.LookupType(f.Object.Type))
Expand Down
4 changes: 3 additions & 1 deletion codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ type ComplexityRoot struct {
{{ if $object.HasResolvers }}
type {{$object.Name}}Resolver interface {
{{ range $field := $object.Fields -}}
{{ $field.ShortResolverDeclaration }}
{{- if $field.IsResolver }}
{{- $field.GoFieldName}}{{ $field.ShortResolverDeclaration }}
{{- end }}
{{ end }}
}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion plugin/resolvergen/resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type {{.ResolverType}} struct {}

{{ range $field := $object.Fields -}}
{{- if $field.IsResolver -}}
func (r *{{lcFirst $object.Name}}Resolver) {{ $field.ShortResolverDeclaration }} {
func (r *{{lcFirst $object.Name}}Resolver) {{$field.GoFieldName}}{{ $field.ShortResolverDeclaration }} {
panic("not implemented")
}
{{ end -}}
Expand Down
39 changes: 39 additions & 0 deletions plugin/stubgen/stubs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package stubgen

import (
"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/99designs/gqlgen/plugin"
)

func New(filename string, typename string) plugin.Plugin {
return &Plugin{filename: filename, typeName: typename}
}

type Plugin struct {
filename string
typeName string
}

var _ plugin.CodeGenerator = &Plugin{}

func (m *Plugin) Name() string {
return "stubgen"
}
func (m *Plugin) GenerateCode(data *codegen.Data) error {
return templates.Render(templates.Options{
PackageName: data.Config.Resolver.Package,
Filename: m.filename,
Data: &ResolverBuild{
Data: data,
TypeName: m.typeName,
},
GeneratedHeader: false,
})
}

type ResolverBuild struct {
*codegen.Data

TypeName string
}
57 changes: 57 additions & 0 deletions plugin/stubgen/stubs.gotpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{ reserveImport "context" }}
{{ reserveImport "fmt" }}
{{ reserveImport "io" }}
{{ reserveImport "strconv" }}
{{ reserveImport "time" }}
{{ reserveImport "sync" }}
{{ reserveImport "errors" }}
{{ reserveImport "bytes" }}

{{ reserveImport "github.com/99designs/gqlgen/handler" }}
{{ reserveImport "github.com/vektah/gqlparser" }}
{{ reserveImport "github.com/vektah/gqlparser/ast" }}
{{ reserveImport "github.com/99designs/gqlgen/graphql" }}
{{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }}

{{ $root := . }}

type {{$root.TypeName}} struct {
{{ range $object := .Objects }}
{{- if $object.HasResolvers }}
{{$object.Name}}Resolver struct {
{{- range $field := $object.Fields }}
{{- if $field.IsResolver }}
{{- $field.GoFieldName}} func{{ $field.ShortResolverDeclaration }}
{{ end }}
{{- end }}
}
{{- end }}
{{- end }}
}

{{ range $object := .Objects -}}
{{- if $object.HasResolvers -}}
func (r *{{$.TypeName}}) {{$object.Name}}() {{ $object.ResolverInterface | ref }} {
return &{{lcFirst $root.TypeName}}{{$object.Name}}{r}
}
{{ end -}}
{{ end }}

{{ range $object := .Objects -}}
{{- if $object.HasResolvers -}}
type {{lcFirst $root.TypeName}}{{$object.Name}} struct { *{{$root.TypeName}} }

{{ range $field := $object.Fields -}}
{{- if $field.IsResolver -}}
func (r *{{lcFirst $root.TypeName}}{{$object.Name}}) {{$field.GoFieldName}}{{ $field.ShortResolverDeclaration }} {
return r.{{$object.Name}}Resolver.{{$field.GoFieldName}}(ctx,
{{- if not $object.Root }}obj,{{end -}}
{{- range $arg := $field.Args}}
{{$arg.VarName}},
{{- end }}
)
}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end }}
3 changes: 2 additions & 1 deletion testdata/gqlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/99designs/gqlgen"
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin/stubgen"
)

func main() {
Expand All @@ -22,7 +23,7 @@ func main() {
os.Exit(2)
}

err = gqlgen.Generate(cfg)
err = gqlgen.Generate(cfg, gqlgen.AddPlugin(stubgen.New(cfg.Exec.Dir()+"/stubs.go", "Stub")))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
Expand Down

0 comments on commit c5e3dd4

Please sign in to comment.