From c5e3dd44959d59bef6830cbe2652498e49b53089 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 7 Feb 2019 20:10:15 +1100 Subject: [PATCH] add stub generation plugin --- codegen/field.go | 5 +-- codegen/generated!.gotpl | 4 ++- plugin/resolvergen/resolver.gotpl | 2 +- plugin/stubgen/stubs.go | 39 +++++++++++++++++++++ plugin/stubgen/stubs.gotpl | 57 +++++++++++++++++++++++++++++++ testdata/gqlgen.go | 3 +- 6 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 plugin/stubgen/stubs.go create mode 100644 plugin/stubgen/stubs.gotpl diff --git a/codegen/field.go b/codegen/field.go index 42dce261004..1c9c129bf28 100644 --- a/codegen/field.go +++ b/codegen/field.go @@ -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)) diff --git a/codegen/generated!.gotpl b/codegen/generated!.gotpl index 5833ee61c3e..31486bd550e 100644 --- a/codegen/generated!.gotpl +++ b/codegen/generated!.gotpl @@ -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 }} diff --git a/plugin/resolvergen/resolver.gotpl b/plugin/resolvergen/resolver.gotpl index 65625f51a5e..7d95e6903ce 100644 --- a/plugin/resolvergen/resolver.gotpl +++ b/plugin/resolvergen/resolver.gotpl @@ -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 -}} diff --git a/plugin/stubgen/stubs.go b/plugin/stubgen/stubs.go new file mode 100644 index 00000000000..f00cf924d73 --- /dev/null +++ b/plugin/stubgen/stubs.go @@ -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 +} diff --git a/plugin/stubgen/stubs.gotpl b/plugin/stubgen/stubs.gotpl new file mode 100644 index 00000000000..d8adbf0b2c6 --- /dev/null +++ b/plugin/stubgen/stubs.gotpl @@ -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 }} diff --git a/testdata/gqlgen.go b/testdata/gqlgen.go index 6b37275a0dc..d51bfc0ce3f 100644 --- a/testdata/gqlgen.go +++ b/testdata/gqlgen.go @@ -9,6 +9,7 @@ import ( "github.com/99designs/gqlgen" "github.com/99designs/gqlgen/codegen/config" + "github.com/99designs/gqlgen/plugin/stubgen" ) func main() { @@ -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)