From 3a5827d430dab85ce0b9144ee9aaa3a4319c1884 Mon Sep 17 00:00:00 2001 From: Ron <38083777+roneli@users.noreply.github.com> Date: Fri, 17 May 2024 00:24:01 +0300 Subject: [PATCH] Fix #2856: resolver receive previous implementation on render (#2886) * pass previous impl to resolver * pass previous only and not default --- plugin/plugin.go | 2 +- plugin/resolvergen/resolver.go | 16 +++++++++------- plugin/resolvergen/resolver_test.go | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index ccb8f8b9346..a5e1ba848d7 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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 } diff --git a/plugin/resolvergen/resolver.go b/plugin/resolvergen/resolver.go index 085c2958470..97a20477165 100644 --- a/plugin/resolvergen/resolver.go +++ b/plugin/resolvergen/resolver.go @@ -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 { @@ -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 } diff --git a/plugin/resolvergen/resolver_test.go b/plugin/resolvergen/resolver_test.go index 7c590b9e4c2..db84a5b3fd7 100644 --- a/plugin/resolvergen/resolver_test.go +++ b/plugin/resolvergen/resolver_test.go @@ -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\")" }