Skip to content

Commit

Permalink
Allow custom resolver filenames using filename_template option
Browse files Browse the repository at this point in the history
This is a squash commit of #1085, required to fix the broken tests and
resolve merge conflicts.
  • Loading branch information
tobias-kuendig authored and vektah committed Jul 26, 2020
1 parent fbfdd41 commit ad675f0
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 21 deletions.
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}.resolvers.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)
}
38 changes: 27 additions & 11 deletions plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,33 @@ func TestLayoutSingleFile(t *testing.T) {
}

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

cfg, err := config.LoadConfig("testdata/followschema/gqlgen.yml")
b, err := ioutil.ReadFile("testdata/followschema/out/schema.resolvers.go")
require.NoError(t, err)
source := string(b)

require.Contains(t, source, "// CustomerResolverType.Resolver implementation")
require.Contains(t, source, "// CustomerResolverType.Name implementation")
require.Contains(t, source, "// AUserHelperFunction implementation")
}

func TestLayoutFollowSchemaWithCustomFilename(t *testing.T) {
testFollowSchemaPersistence(t, "testdata/filetemplate")

b, err := ioutil.ReadFile("testdata/filetemplate/out/schema.custom.go")
require.NoError(t, err)
source := string(b)

require.Contains(t, source, "// CustomerResolverType.Resolver implementation")
require.Contains(t, source, "// CustomerResolverType.Name implementation")
require.Contains(t, source, "// AUserHelperFunction implementation")
}

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

cfg, err := config.LoadConfig(dir + "/gqlgen.yml")
require.NoError(t, err)
p := Plugin{}

Expand All @@ -45,15 +69,7 @@ func TestLayoutFollowSchema(t *testing.T) {
}

require.NoError(t, p.GenerateCode(data))
assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/testdata/followschema/out")

b, err := ioutil.ReadFile("testdata/followschema/out/schema.resolvers.go")
require.NoError(t, err)
source := string(b)

require.Contains(t, source, "// CustomerResolverType.Resolver implementation")
require.Contains(t, source, "// CustomerResolverType.Name implementation")
require.Contains(t, source, "// AUserHelperFunction implementation")
assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/"+dir+"/out")
}

func assertNoErrors(t *testing.T, pkg string) {
Expand Down
16 changes: 16 additions & 0 deletions plugin/resolvergen/testdata/filetemplate/gqlgen.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/filetemplate/out
filename_template: "{name}.custom.go"

models:
Resolver:
model: github.com/99designs/gqlgen/plugin/resolvergen/testdata/singlefile/out.Resolver
14 changes: 14 additions & 0 deletions plugin/resolvergen/testdata/filetemplate/out/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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)
}
7 changes: 7 additions & 0 deletions plugin/resolvergen/testdata/filetemplate/out/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package customresolver

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

type CustomResolverType struct{}
41 changes: 41 additions & 0 deletions plugin/resolvergen/testdata/filetemplate/out/schema.custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package customresolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
"context"

customresolver "github.com/99designs/gqlgen/plugin/resolvergen/testdata/singlefile/out"
)

func (r *queryCustomResolverType) Resolver(ctx context.Context) (*customresolver.Resolver, error) {
// CustomerResolverType.Resolver implementation
return nil, nil
}

func (r *resolverCustomResolverType) Name(ctx context.Context, obj *customresolver.Resolver) (string, error) {
// CustomerResolverType.Name implementation
return "", nil
}

// Query returns customresolver.QueryResolver implementation.
func (r *CustomResolverType) Query() customresolver.QueryResolver { return &queryCustomResolverType{r} }

// Resolver returns customresolver.ResolverResolver implementation.
func (r *CustomResolverType) Resolver() customresolver.ResolverResolver {
return &resolverCustomResolverType{r}
}

type queryCustomResolverType struct{ *CustomResolverType }
type resolverCustomResolverType struct{ *CustomResolverType }

// !!! 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.
func AUserHelperFunction() {
// AUserHelperFunction implementation
}

0 comments on commit ad675f0

Please sign in to comment.