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

Make the resolver implementation configurable via a new template resolver.gotpl #2720

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codegen/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ResolverConfig struct {
Layout ResolverLayout `yaml:"layout,omitempty"`
DirName string `yaml:"dir"`
OmitTemplateComment bool `yaml:"omit_template_comment,omitempty"`
ResolverTemplate string `yaml:"resolver_template,omitempty"`
}

type ResolverLayout string
Expand Down
2 changes: 2 additions & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ resolver:
filename_template: "{name}.resolvers.go"
# Optional: turn on to not generate template comments above resolvers
# omit_template_comment: false
# Optional: Pass in a path to a new gotpl template to use for generating resolvers
# resolver_template: [your/path/resolver.gotpl]

# Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models
# struct_tag: json
Expand Down
16 changes: 16 additions & 0 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (m *Plugin) generateSingleFile(data *codegen.Data) error {
OmitTemplateComment: data.Config.Resolver.OmitTemplateComment,
}

if data.Config.Resolver.ResolverTemplate != "" {
resolverTemplate = readResolverTemplate(data.Config.Resolver.ResolverTemplate)
}

return templates.Render(templates.Options{
PackageName: data.Config.Resolver.Package,
FileNotice: `// THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.`,
Expand Down Expand Up @@ -159,6 +163,10 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
file.RemainingSource = rewriter.RemainingSource(filename)
}

if data.Config.Resolver.ResolverTemplate != "" {
resolverTemplate = readResolverTemplate(data.Config.Resolver.ResolverTemplate)
}

for filename, file := range files {
resolverBuild := &ResolverBuild{
File: file,
Expand Down Expand Up @@ -257,3 +265,11 @@ func gqlToResolverName(base string, gqlname, filenameTmpl string) string {
filename := strings.ReplaceAll(filenameTmpl, "{name}", strings.TrimSuffix(gqlname, ext))
return filepath.Join(base, filename)
}

func readResolverTemplate(customResolverTemplate string) string {
contentBytes, err := os.ReadFile(customResolverTemplate)
if err != nil {
panic(err)
}
return string(contentBytes)
}
16 changes: 16 additions & 0 deletions plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ func TestOmitTemplateComment(t *testing.T) {
assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/testdata/omit_template_comment/out")
}

func TestCustomResolverTemplate(t *testing.T) {
_ = syscall.Unlink("testdata/resolvertemplate/out/resolver.go")
cfg, err := config.LoadConfig("testdata/resolvertemplate/gqlgen.yml")
require.NoError(t, err)
p := Plugin{}

require.NoError(t, cfg.Init())

data, err := codegen.BuildData(cfg)
if err != nil {
panic(err)
}

require.NoError(t, p.GenerateCode(data))
}

func testFollowSchemaPersistence(t *testing.T, dir string) {
_ = syscall.Unlink(dir + "/out/resolver.go")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{{ reserveImport "context" }}
{{ reserveImport "fmt" }}
{{ reserveImport "io" }}
{{ reserveImport "strconv" }}
{{ reserveImport "time" }}
{{ reserveImport "sync" }}
{{ reserveImport "errors" }}
{{ reserveImport "bytes" }}
{{ reserveImport "encoding/json" }}

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

{{ .Imports }}

{{ if .HasRoot }}
type {{.ResolverType}} struct {}
{{ end }}

{{ range $resolver := .Resolvers -}}
{{ if $resolver.Comment -}}
// {{ $resolver.Comment }}
{{- else if not $.OmitTemplateComment -}}
// {{ $resolver.Field.GoFieldName }} is the resolver for the {{ $resolver.Field.Name }} field.
{{- end }}
func (r *{{lcFirst $resolver.Object.Name}}{{ucFirst $.ResolverType}}) {{$resolver.Field.GoFieldName}}{{ with $resolver.PrevDecl }}{{ $resolver.Field.ShortResolverSignature .Type }}{{ else }}{{ $resolver.Field.ShortResolverDeclaration }}{{ end }}{
// Custom Resolver implementation
panic(fmt.Errorf("custom Resolver not implemented: {{ $resolver.Field.GoFieldName }} - {{lcFirst $resolver.Field.GoFieldName }}"))
}

{{ end }}

{{ range $object := .Objects -}}
{{ if not $.OmitTemplateComment -}}
// {{ucFirst $object.Name}} returns {{ $object.ResolverInterface | ref }} implementation.
{{- end }}
func (r *{{$.ResolverType}}) {{ucFirst $object.Name}}() {{ $object.ResolverInterface | ref }} { return &{{lcFirst $object.Name}}{{ucFirst $.ResolverType}}{r} }
{{ end }}

{{ range $object := .Objects -}}
type {{lcFirst $object.Name}}{{ucFirst $.ResolverType}} struct { *{{$.ResolverType}} }
{{ end }}

{{ if (ne .RemainingSource "") }}
// !!! WARNING !!!
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
// one last chance to move it out of harms way if you want. There are two reasons this happens:
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
// it when you're done.
// - You have helper methods in this file. Move them out to keep these resolver files clean.
{{ .RemainingSource }}
{{ end }}
17 changes: 17 additions & 0 deletions plugin/resolvergen/testdata/resolvertemplate/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
schema:
- "testdata/schema.graphql"

exec:
filename: testdata/singlefile/out/ignored.go
model:
filename: testdata/singlefile/out/generated.go
resolver:
type: CustomResolverType
layout: follow-schema
dir: testdata/resolvertemplate/out
filename_template: "{name}.resolvers.go"
resolver_template: "testdata/resolvertemplate/customResolverTemplate.gotpl"

models:
Resolver:
model: github.com/99designs/gqlgen/plugin/resolvergen/testdata/singlefile/out.Resolver
7 changes: 7 additions & 0 deletions plugin/resolvergen/testdata/resolvertemplate/out/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package out

// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.

type CustomResolverType struct{}

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