From c355813b241af591d1d31b774ed84b4e95c166ea Mon Sep 17 00:00:00 2001 From: Haily Nguyen Date: Thu, 12 Sep 2024 13:06:49 -0700 Subject: [PATCH] make rewrite default for single-file mode and update ALL generate test cases --- _examples/type-system-extension/gqlgen.yml | 3 --- codegen/testserver/followschema/gqlgen.yml | 5 +--- .../testserver/nullabledirectives/gqlgen.yml | 5 +--- codegen/testserver/singlefile/gqlgen.yml | 4 --- integration/server/gqlgen.yml | 3 --- plugin/resolvergen/resolver.go | 26 ++++++++++++++----- plugin/resolvergen/resolver.gotpl | 2 ++ .../testdata/singlefile/out/resolver.go | 7 ++--- 8 files changed, 28 insertions(+), 27 deletions(-) diff --git a/_examples/type-system-extension/gqlgen.yml b/_examples/type-system-extension/gqlgen.yml index bc7a31dba05..eba540c1f86 100644 --- a/_examples/type-system-extension/gqlgen.yml +++ b/_examples/type-system-extension/gqlgen.yml @@ -10,6 +10,3 @@ exec: filename: generated.go model: filename: models_gen.go -resolver: - filename: resolver.go - type: Resolver diff --git a/codegen/testserver/followschema/gqlgen.yml b/codegen/testserver/followschema/gqlgen.yml index 5119a2f5038..075f4b81cc4 100644 --- a/codegen/testserver/followschema/gqlgen.yml +++ b/codegen/testserver/followschema/gqlgen.yml @@ -8,10 +8,7 @@ exec: model: filename: models-gen.go package: followschema -resolver: - filename: resolver.go - package: followschema - type: Resolver + autobind: - "github.com/99designs/gqlgen/codegen/testserver" diff --git a/codegen/testserver/nullabledirectives/gqlgen.yml b/codegen/testserver/nullabledirectives/gqlgen.yml index 657e9d04123..0090b822b26 100644 --- a/codegen/testserver/nullabledirectives/gqlgen.yml +++ b/codegen/testserver/nullabledirectives/gqlgen.yml @@ -8,9 +8,6 @@ exec: model: filename: generated/models/models-gen.go package: models -resolver: - filename: generated/resolvers/resolver.go - package: resolver - type: Resolver + call_argument_directives_with_null: true diff --git a/codegen/testserver/singlefile/gqlgen.yml b/codegen/testserver/singlefile/gqlgen.yml index afd3fb90dd8..d621903f283 100644 --- a/codegen/testserver/singlefile/gqlgen.yml +++ b/codegen/testserver/singlefile/gqlgen.yml @@ -7,10 +7,6 @@ exec: model: filename: models-gen.go package: singlefile -resolver: - filename: resolver.go - package: singlefile - type: Resolver autobind: - "github.com/99designs/gqlgen/codegen/testserver" diff --git a/integration/server/gqlgen.yml b/integration/server/gqlgen.yml index 427490c7efc..9195079be1e 100644 --- a/integration/server/gqlgen.yml +++ b/integration/server/gqlgen.yml @@ -5,9 +5,6 @@ exec: filename: generated.go model: filename: models-go/generated.go -resolver: - filename: resolver.go - type: Resolver struct_tag: json diff --git a/plugin/resolvergen/resolver.go b/plugin/resolvergen/resolver.go index 38138d52014..c50b4763668 100644 --- a/plugin/resolvergen/resolver.go +++ b/plugin/resolvergen/resolver.go @@ -53,26 +53,40 @@ func (m *Plugin) GenerateCode(data *codegen.Data) error { func (m *Plugin) generateSingleFile(data *codegen.Data) error { file := File{} - - if _, err := os.Stat(data.Config.Resolver.Filename); err == nil { - // file already exists and we do not support updating resolvers with layout = single so just return - return nil + rewriter, err := rewrite.New(data.Config.Resolver.Dir()) + if err != nil { + return err } for _, o := range data.Objects { if o.HasResolvers() { + caser := cases.Title(language.English, cases.NoLower) + rewriter.MarkStructCopied(templates.LcFirst(o.Name) + templates.UcFirst(data.Config.Resolver.Type)) + rewriter.GetMethodBody(data.Config.Resolver.Type, caser.String(o.Name)) + file.Objects = append(file.Objects, o) } + for _, f := range o.Fields { if !f.IsResolver { continue } - resolver := Resolver{o, f, nil, "", `panic("not implemented")`, nil} + structName := templates.LcFirst(o.Name) + templates.UcFirst(data.Config.Resolver.Type) + comment := strings.TrimSpace(strings.TrimLeft(rewriter.GetMethodComment(structName, f.GoFieldName), `\`)) + implementation := strings.TrimSpace(rewriter.GetMethodBody(structName, f.GoFieldName)) + + resolver := Resolver{o, f, rewriter.GetPrevDecl(structName, f.GoFieldName), comment, implementation, nil} file.Resolvers = append(file.Resolvers, &resolver) } } + if _, err := os.Stat(data.Config.Resolver.Filename); err == nil { + file.name = data.Config.Resolver.Filename + file.imports = rewriter.ExistingImports(file.name) + file.RemainingSource = rewriter.RemainingSource(file.name) + } + resolverBuild := &ResolverBuild{ File: &file, PackageName: data.Config.Resolver.Package, @@ -88,7 +102,7 @@ func (m *Plugin) generateSingleFile(data *codegen.Data) error { 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.`, + FileNotice: `// THIS CODE WILL BE UPDATED WITH SCHEMA CHANGES. PREVIOUS IMPLEMENTATION FOR SCHEMA CHANGES WILL BE KEPT IN THE COMMENT SECTION. IMPLEMENTATION FOR UNCHANGED SCHEMA WILL BE KEPT.`, Filename: data.Config.Resolver.Filename, Data: resolverBuild, Packages: data.Config.Packages, diff --git a/plugin/resolvergen/resolver.gotpl b/plugin/resolvergen/resolver.gotpl index c25bd1d5627..ad6c1085819 100644 --- a/plugin/resolvergen/resolver.gotpl +++ b/plugin/resolvergen/resolver.gotpl @@ -48,5 +48,7 @@ // - 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. + /* {{ .RemainingSource }} + */ {{ end }} diff --git a/plugin/resolvergen/testdata/singlefile/out/resolver.go b/plugin/resolvergen/testdata/singlefile/out/resolver.go index 844b077c4ac..614c66cf18b 100644 --- a/plugin/resolvergen/testdata/singlefile/out/resolver.go +++ b/plugin/resolvergen/testdata/singlefile/out/resolver.go @@ -1,21 +1,22 @@ package customresolver -// THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES. +// THIS CODE WILL BE UPDATED WITH SCHEMA CHANGES. PREVIOUS IMPLEMENTATION FOR SCHEMA CHANGES WILL BE KEPT IN THE COMMENT SECTION. IMPLEMENTATION FOR UNCHANGED SCHEMA WILL BE KEPT. import ( "context" + "fmt" ) type CustomResolverType struct{} // Resolver is the resolver for the resolver field. func (r *queryCustomResolverType) Resolver(ctx context.Context) (*Resolver, error) { - panic("not implemented") + panic(fmt.Errorf("not implemented: Resolver - resolver")) } // Name is the resolver for the name field. func (r *resolverCustomResolverType) Name(ctx context.Context, obj *Resolver) (string, error) { - panic("not implemented") + panic(fmt.Errorf("not implemented: Name - name")) } // Query returns QueryResolver implementation.