Skip to content

Commit cf1607a

Browse files
shrdlu68Brian Kamotho
andauthored
Add ability to customize resolvergen behavior using additional plugins (#2516)
* Add ability to customize resolvergen behavior using additional plugins * Add field.GoResultName() --------- Co-authored-by: Brian Kamotho <[email protected]>
1 parent 356f4f9 commit cf1607a

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

api/generate.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ func Generate(cfg *config.Config, option ...Option) error {
8383
}
8484
}
8585
// Merge again now that the generated models have been injected into the typemap
86-
data, err := codegen.BuildData(cfg)
86+
data_plugins := make([]interface{}, len(plugins))
87+
for index := range plugins {
88+
data_plugins[index] = plugins[index]
89+
}
90+
data, err := codegen.BuildData(cfg, data_plugins...)
8791
if err != nil {
8892
return fmt.Errorf("merging type systems failed: %w", err)
8993
}

codegen/data.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Data struct {
3434
MutationRoot *Object
3535
SubscriptionRoot *Object
3636
AugmentedSources []AugmentedSource
37+
Plugins []interface{}
3738
}
3839

3940
func (d *Data) HasEmbeddableSources() bool {
@@ -76,7 +77,7 @@ func (d *Data) Directives() DirectiveList {
7677
return res
7778
}
7879

79-
func BuildData(cfg *config.Config) (*Data, error) {
80+
func BuildData(cfg *config.Config, plugins ...interface{}) (*Data, error) {
8081
// We reload all packages to allow packages to be compared correctly.
8182
cfg.ReloadAllPackages()
8283

@@ -105,6 +106,7 @@ func BuildData(cfg *config.Config) (*Data, error) {
105106
AllDirectives: dataDirectives,
106107
Schema: b.Schema,
107108
Interfaces: map[string]*Interface{},
109+
Plugins: plugins,
108110
}
109111

110112
for _, schemaType := range b.Schema.Types {

codegen/field.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,14 @@ func (f *Field) ResolverType() string {
503503
return fmt.Sprintf("%s().%s(%s)", f.Object.Definition.Name, f.GoFieldName, f.CallArgs())
504504
}
505505

506+
func (f *Field) IsInputObject() bool {
507+
return f.Object.Kind == ast.InputObject
508+
}
509+
510+
func (f *Field) IsRoot() bool {
511+
return f.Object.Root
512+
}
513+
506514
func (f *Field) ShortResolverDeclaration() string {
507515
return f.ShortResolverSignature(nil)
508516
}
@@ -544,6 +552,13 @@ func (f *Field) ShortResolverSignature(ft *goast.FuncType) string {
544552
return res
545553
}
546554

555+
func (f *Field) GoResultName() (string, bool) {
556+
name := fmt.Sprintf("%v", f.TypeReference.GO)
557+
splits := strings.Split(name, "/")
558+
559+
return splits[len(splits)-1], strings.HasPrefix(name, "[]")
560+
}
561+
547562
func (f *Field) ComplexitySignature() string {
548563
res := "func(childComplexity int"
549564
for _, arg := range f.Args {

codegen/object.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ func (o *Object) Description() string {
153153
return o.Definition.Description
154154
}
155155

156+
func (o *Object) HasField(name string) bool {
157+
for _, f := range o.Fields {
158+
if f.Name == name {
159+
return true
160+
}
161+
}
162+
163+
return false
164+
}
165+
156166
func (os Objects) ByName(name string) *Object {
157167
for i, o := range os {
158168
if strings.EqualFold(o.Definition.Name, name) {

plugin/plugin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ type EarlySourceInjector interface {
2929
type LateSourceInjector interface {
3030
InjectSourceLate(schema *ast.Schema) *ast.Source
3131
}
32+
33+
// Implementer is used to generate code inside resolvers
34+
type ResolverImplementer interface {
35+
Implement(field *codegen.Field) string
36+
}

plugin/resolvergen/resolver.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,29 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
121121

122122
structName := templates.LcFirst(o.Name) + templates.UcFirst(data.Config.Resolver.Type)
123123
comment := strings.TrimSpace(strings.TrimLeft(rewriter.GetMethodComment(structName, f.GoFieldName), `\`))
124+
124125
if comment == "" {
125126
comment = fmt.Sprintf("%v is the resolver for the %v field.", f.GoFieldName, f.Name)
126127
}
127128
implementation := strings.TrimSpace(rewriter.GetMethodBody(structName, f.GoFieldName))
128129
if implementation == "" {
129-
implementation = fmt.Sprintf("panic(fmt.Errorf(\"not implemented: %v - %v\"))", f.GoFieldName, f.Name)
130+
// Check for Implementer Plugin
131+
var resolver_implementer plugin.ResolverImplementer
132+
var exists bool
133+
for _, p := range data.Plugins {
134+
if p_cast, ok := p.(plugin.ResolverImplementer); ok {
135+
resolver_implementer = p_cast
136+
exists = true
137+
break
138+
}
139+
}
140+
141+
if exists {
142+
implementation = resolver_implementer.Implement(f)
143+
} else {
144+
implementation = fmt.Sprintf("panic(fmt.Errorf(\"not implemented: %v - %v\"))", f.GoFieldName, f.Name)
145+
}
146+
130147
}
131148

132149
resolver := Resolver{o, f, rewriter.GetPrevDecl(structName, f.GoFieldName), comment, implementation}

0 commit comments

Comments
 (0)