Skip to content

Commit

Permalink
Make an optional resolver.gotpl ResolverTemplate to implement a custo…
Browse files Browse the repository at this point in the history
…m resolver
  • Loading branch information
penard-cb committed Jul 19, 2023
1 parent cccc738 commit ad1127d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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
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)
}

0 comments on commit ad1127d

Please sign in to comment.