Skip to content

Commit

Permalink
Fix #2856: resolver receive previous implementation on render (#2886)
Browse files Browse the repository at this point in the history
* pass previous impl to resolver

* pass previous only and not default
  • Loading branch information
roneli committed May 16, 2024
1 parent e012530 commit 3a5827d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ type LateSourceInjector interface {

// ResolverImplementer is used to generate code inside resolvers
type ResolverImplementer interface {
Implement(field *codegen.Field) string
Implement(prevImplementation string, field *codegen.Field) string
}
16 changes: 9 additions & 7 deletions plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,9 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
if !f.IsResolver {
continue
}

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 == "" {
// use default implementation, if no implementation was previously used
implementation = fmt.Sprintf("panic(fmt.Errorf(\"not implemented: %v - %v\"))", f.GoFieldName, f.Name)
}
resolver := Resolver{o, f, rewriter.GetPrevDecl(structName, f.GoFieldName), comment, implementation, nil}
var implExists bool
for _, p := range data.Plugins {
Expand Down Expand Up @@ -257,13 +252,20 @@ type Resolver struct {
PrevDecl *ast.FuncDecl
Comment string
ImplementationStr string
ImplementationRender func(r *codegen.Field) string
ImplementationRender func(prevImplementation string, r *codegen.Field) string
}

func (r *Resolver) Implementation() string {
if r.ImplementationRender != nil {
return r.ImplementationRender(r.Field)
// use custom implementation
return r.ImplementationRender(r.ImplementationStr, r.Field)
}
// if not implementation was previously used, use default implementation
if r.ImplementationStr == "" {
// use default implementation, if no implementation was previously used
return fmt.Sprintf("panic(fmt.Errorf(\"not implemented: %v - %v\"))", r.Field.GoFieldName, r.Field.Name)
}
// use previously used implementation
return r.ImplementationStr
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ func assertNoErrors(t *testing.T, pkg string) {

type implementorTest struct{}

func (i *implementorTest) Implement(field *codegen.Field) string {
func (i *implementorTest) Implement(_ string, _ *codegen.Field) string {
return "panic(\"implementor implemented me\")"
}

0 comments on commit 3a5827d

Please sign in to comment.