Skip to content

Commit

Permalink
make rewrite default for resolver layout single-file (#3243)
Browse files Browse the repository at this point in the history
  • Loading branch information
hailyngx authored Sep 13, 2024
1 parent 1855758 commit 6b9e40e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 19 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
2 changes: 1 addition & 1 deletion codegen/testserver/followschema/resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package followschema

// 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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package resolver

// 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"
Expand All @@ -19,3 +19,13 @@ func (r *queryResolver) DirectiveSingleNullableArg(ctx context.Context, arg1 *st
func (r *Resolver) Query() nullabledirectives.QueryResolver { return &queryResolver{r} }

type queryResolver struct{ *Resolver }

// !!! 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.
/*
type Resolver struct{}
*/
2 changes: 1 addition & 1 deletion codegen/testserver/singlefile/resolver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package singlefile

// 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"
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
32 changes: 25 additions & 7 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,44 @@ 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}
file.Resolvers = append(file.Resolvers, &resolver)
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))
if implementation != "" {
resolver := Resolver{o, f, rewriter.GetPrevDecl(structName, f.GoFieldName), comment, implementation, nil}
file.Resolvers = append(file.Resolvers, &resolver)
} else {
resolver := Resolver{o, f, nil, "", `panic("not implemented")`, 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 +106,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 6b9e40e

Please sign in to comment.