diff --git a/codegen/config/resolver.go b/codegen/config/resolver.go index 9859d6e3a00..cd03f188729 100644 --- a/codegen/config/resolver.go +++ b/codegen/config/resolver.go @@ -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 diff --git a/docs/content/config.md b/docs/content/config.md index b84013cfb9c..b6a0732f8f6 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -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 diff --git a/plugin/resolvergen/resolver.go b/plugin/resolvergen/resolver.go index f4aefae20cf..6739a325dc8 100644 --- a/plugin/resolvergen/resolver.go +++ b/plugin/resolvergen/resolver.go @@ -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{} } @@ -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{} } @@ -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) } diff --git a/plugin/resolvergen/resolver_test.go b/plugin/resolvergen/resolver_test.go index cc399e53ec6..2a11b98f614 100644 --- a/plugin/resolvergen/resolver_test.go +++ b/plugin/resolvergen/resolver_test.go @@ -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) @@ -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 | diff --git a/plugin/resolvergen/testdata/followschema/gqlgen.customfilename.yml b/plugin/resolvergen/testdata/followschema/gqlgen.customfilename.yml new file mode 100644 index 00000000000..dceaff5ce9f --- /dev/null +++ b/plugin/resolvergen/testdata/followschema/gqlgen.customfilename.yml @@ -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 diff --git a/plugin/resolvergen/testdata/followschema/out/model.go b/plugin/resolvergen/testdata/followschema/out/model.go deleted file mode 100644 index 11d6d254209..00000000000 --- a/plugin/resolvergen/testdata/followschema/out/model.go +++ /dev/null @@ -1,14 +0,0 @@ -package customresolver - -import "context" - -type Resolver struct { -} - -type QueryResolver interface { - Resolver(ctx context.Context) (*Resolver, error) -} - -type ResolverResolver interface { - Name(ctx context.Context, obj *Resolver) (string, error) -} diff --git a/plugin/resolvergen/testdata/followschema/out/resolver.go b/plugin/resolvergen/testdata/followschema/out/resolver.go deleted file mode 100644 index 61296288110..00000000000 --- a/plugin/resolvergen/testdata/followschema/out/resolver.go +++ /dev/null @@ -1,6 +0,0 @@ -// This file will not be regenerated automatically. -// -// It serves as dependency injection for your app, add any dependencies you require here. -package customresolver - -type CustomResolverType struct{}