From ad1127d33ea11879422afa4106a9dec9cccc1a7d Mon Sep 17 00:00:00 2001 From: David Pena Date: Wed, 19 Jul 2023 14:11:22 -0400 Subject: [PATCH] Make an optional resolver.gotpl ResolverTemplate to implement a custom resolver --- codegen/config/resolver.go | 1 + plugin/resolvergen/resolver.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/codegen/config/resolver.go b/codegen/config/resolver.go index a3ec38f8e20..cb5fb72b36a 100644 --- a/codegen/config/resolver.go +++ b/codegen/config/resolver.go @@ -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 diff --git a/plugin/resolvergen/resolver.go b/plugin/resolvergen/resolver.go index 09cfbab63bb..215bc469df3 100644 --- a/plugin/resolvergen/resolver.go +++ b/plugin/resolvergen/resolver.go @@ -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.`, @@ -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, @@ -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) +}