Skip to content

Commit

Permalink
make rewrite default for single-file mode and update ALL generate tes…
Browse files Browse the repository at this point in the history
…t cases
  • Loading branch information
Haily Nguyen committed Sep 12, 2024
1 parent 814f7c7 commit c355813
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 27 deletions.
3 changes: 0 additions & 3 deletions _examples/type-system-extension/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ exec:
filename: generated.go
model:
filename: models_gen.go
resolver:
filename: resolver.go
type: Resolver
5 changes: 1 addition & 4 deletions codegen/testserver/followschema/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 1 addition & 4 deletions codegen/testserver/nullabledirectives/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions codegen/testserver/singlefile/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions integration/server/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ exec:
filename: generated.go
model:
filename: models-go/generated.go
resolver:
filename: resolver.go
type: Resolver

struct_tag: json

Expand Down
26 changes: 20 additions & 6 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions plugin/resolvergen/resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
7 changes: 4 additions & 3 deletions plugin/resolvergen/testdata/singlefile/out/resolver.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit c355813

Please sign in to comment.