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

Allow custom resolver filenames using filename_template option #1085

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 6 additions & 5 deletions codegen/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
)

type ResolverConfig struct {
Filename string `yaml:"filename,omitempty"`
Package string `yaml:"package,omitempty"`
Type string `yaml:"type,omitempty"`
Layout ResolverLayout `yaml:"layout,omitempty"`
DirName string `yaml:"dir"`
Filename string `yaml:"filename,omitempty"`
FilenameTemplate string `yaml:"filename_template,omitempty"`
Package string `yaml:"package,omitempty"`
Type string `yaml:"type,omitempty"`
Layout ResolverLayout `yaml:"layout,omitempty"`
DirName string `yaml:"dir"`
}

type ResolverLayout string
Expand Down
1 change: 1 addition & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ resolver:
layout: follow-schema
dir: graph
package: graph
filename_template: "{name}.custom.go"

# Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models
# struct_tag: json
Expand Down
13 changes: 8 additions & 5 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {

for _, o := range data.Objects {
if o.HasResolvers() {
fn := gqlToResolverName(data.Config.Resolver.Dir(), o.Position.Src.Name)
fn := gqlToResolverName(data.Config.Resolver.Dir(), o.Position.Src.Name, data.Config.Resolver.FilenameTemplate)
if files[fn] == nil {
files[fn] = &File{}
}
Expand All @@ -109,7 +109,7 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
}

resolver := Resolver{o, f, implementation}
fn := gqlToResolverName(data.Config.Resolver.Dir(), f.Position.Src.Name)
fn := gqlToResolverName(data.Config.Resolver.Dir(), f.Position.Src.Name, data.Config.Resolver.FilenameTemplate)
if files[fn] == nil {
files[fn] = &File{}
}
Expand Down Expand Up @@ -196,9 +196,12 @@ type Resolver struct {
Implementation string
}

func gqlToResolverName(base string, gqlname string) string {
func gqlToResolverName(base string, gqlname, filenameTmpl string) string {
gqlname = filepath.Base(gqlname)
ext := filepath.Ext(gqlname)

return filepath.Join(base, strings.TrimSuffix(gqlname, ext)+".resolvers.go")
if filenameTmpl == "" {
filenameTmpl = "{name}.resolvers.go"
}
filename := strings.ReplaceAll(filenameTmpl, "{name}", strings.TrimSuffix(gqlname, ext))
return filepath.Join(base, filename)
}
24 changes: 24 additions & 0 deletions plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func TestLayoutSingleFile(t *testing.T) {

func TestLayoutFollowSchema(t *testing.T) {
_ = syscall.Unlink("testdata/followschema/out/resolver.go")
_ = syscall.Unlink("testdata/followschema/out/schema.custom.go")
_ = syscall.Unlink("testdata/followschema/out/schema.resolvers.go")

cfg, err := config.LoadConfig("testdata/followschema/gqlgen.yml")
require.NoError(t, err)
Expand All @@ -56,6 +58,28 @@ func TestLayoutFollowSchema(t *testing.T) {
require.Contains(t, source, "// AUserHelperFunction implementation")
}

func TestLayoutFollowSchemaWithCustomFilename(t *testing.T) {
_ = syscall.Unlink("testdata/followschema/out/resolver.go")
_ = syscall.Unlink("testdata/followschema/out/schema.custom.go")
_ = syscall.Unlink("testdata/followschema/out/schema.resolvers.go")

cfg, err := config.LoadConfig("testdata/followschema/gqlgen.customfilename.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))
assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/testdata/followschema/out")

require.FileExists(t, "testdata/followschema/out/schema.custom.go")
}

func assertNoErrors(t *testing.T, pkg string) {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName |
Expand Down
16 changes: 16 additions & 0 deletions plugin/resolvergen/testdata/followschema/gqlgen.customfilename.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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/followschema/out
filename_template: "{name}.custom.go"

models:
Resolver:
model: github.com/99designs/gqlgen/plugin/resolvergen/testdata/singlefile/out.Resolver
14 changes: 0 additions & 14 deletions plugin/resolvergen/testdata/followschema/out/model.go

This file was deleted.

6 changes: 0 additions & 6 deletions plugin/resolvergen/testdata/followschema/out/resolver.go

This file was deleted.