forked from web-ridge/gqlgen-sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_plugin.go
362 lines (312 loc) · 10.3 KB
/
resolver_plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package gbgen
import (
"fmt"
"path"
"strings"
"github.com/rs/zerolog/log"
"github.com/iancoleman/strcase"
"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/plugin"
"github.com/web-ridge/gqlgen-sqlboiler/v3/templates"
)
func NewResolverPlugin(output, backend, frontend Config, resolverPluginConfig ResolverPluginConfig) plugin.Plugin {
return &ResolverPlugin{
output: output,
backend: backend,
frontend: frontend,
pluginConfig: resolverPluginConfig,
rootImportPath: getRootImportPath(),
}
}
type AuthorizationScope struct {
ImportPath string
ImportAlias string
ScopeResolverName string
BoilerColumnName string
AddHook func(model *BoilerModel, resolver *Resolver, templateKey string) bool
}
type ResolverPluginConfig struct {
AuthorizationScopes []*AuthorizationScope
}
type ResolverPlugin struct {
output Config
backend Config
frontend Config
pluginConfig ResolverPluginConfig
rootImportPath string
}
var _ plugin.CodeGenerator = &ResolverPlugin{}
func (m *ResolverPlugin) Name() string {
return "resolver-webridge"
}
func (m *ResolverPlugin) GenerateCode(data *codegen.Data) error {
if !data.Config.Resolver.IsDefined() {
return nil
}
// gqlgenTemplates.CurrentImports = &gqlgenTemplates.Imports{}
// Get all models information
log.Debug().Msg("[resolver] get boiler models")
boilerModels, _ := GetBoilerModels(m.backend.Directory)
log.Debug().Msg("[resolver] get models with information")
models := GetModelsWithInformation(m.backend, nil, data.Config, boilerModels, nil)
log.Debug().Msg("[resolver] generate file")
// switch data.Config.Resolver.Layout {
// case config.LayoutSingleFile:
err := m.generateSingleFile(data, models, boilerModels)
return err
//case config.LayoutFollowSchema:
// return m.generatePerSchema(data, models, boilerModels)
//}
//log.Debug().Msg("[resolver] generated files")
//return nil
}
func (m *ResolverPlugin) generateSingleFile(data *codegen.Data, models []*Model, _ []*BoilerModel) error {
file := File{}
file.Imports = append(file.Imports, Import{
Alias: ".",
ImportPath: path.Join(m.rootImportPath, m.output.Directory),
})
file.Imports = append(file.Imports, Import{
Alias: "dm",
ImportPath: path.Join(m.rootImportPath, m.backend.Directory),
})
file.Imports = append(file.Imports, Import{
Alias: "fm",
ImportPath: path.Join(m.rootImportPath, m.frontend.Directory),
})
file.Imports = append(file.Imports, Import{
Alias: "gm",
ImportPath: buildImportPath(m.rootImportPath, data.Config.Exec.ImportPath()),
})
for _, scope := range m.pluginConfig.AuthorizationScopes {
file.Imports = append(file.Imports, Import{
Alias: scope.ImportAlias,
ImportPath: scope.ImportPath,
})
}
for _, o := range data.Objects {
if o.HasResolvers() {
file.Objects = append(file.Objects, o)
}
for _, f := range o.Fields {
if !f.IsResolver {
continue
}
resolver := &Resolver{
Object: o,
Field: f,
Implementation: `panic("not implemented yet")`,
}
enhanceResolver(resolver, models)
if resolver.Model.BoilerModel != nil && resolver.Model.BoilerModel.Name != "" {
file.Resolvers = append(file.Resolvers, resolver)
} else if resolver.Field.GoFieldName != "Node" {
log.Debug().Str("resolver", resolver.Object.Name).Str("field", resolver.Field.GoFieldName).Msg(
"skipping resolver since no model found")
}
}
}
resolverBuild := &ResolverBuild{
File: &file,
PackageName: data.Config.Resolver.Package,
ResolverType: data.Config.Resolver.Type,
HasRoot: false,
Models: models,
AuthorizationScopes: m.pluginConfig.AuthorizationScopes,
}
templateName := "generated_resolver.gotpl"
templateContent, err := getTemplateContent(templateName)
if err != nil {
log.Err(err).Msg("error when reading " + templateName)
return err
}
return templates.WriteTemplateFile(data.Config.Resolver.Filename, templates.Options{
Template: templateContent,
PackageName: data.Config.Resolver.Package,
Data: resolverBuild,
})
}
func buildImportPath(rootImportPath, directory string) string {
index := strings.Index(directory, rootImportPath)
if index > 0 {
return directory[index:]
}
return directory
}
type ResolverBuild struct {
*File
HasRoot bool
PackageName string
ResolverType string
Models []*Model
AuthorizationScopes []*AuthorizationScope
TryHook func(string) bool
}
type File struct {
// These are separated because the type definition of the resolver object may live in a different file from the
// resolver method implementations, for example when extending a type in a different graphql schema file
Objects []*codegen.Object
Resolvers []*Resolver
Imports []Import
RemainingSource string
}
type Resolver struct {
Object *codegen.Object
Field *codegen.Field
Implementation string
IsSingle bool
IsList bool
IsListForward bool
IsListBackward bool
IsCreate bool
IsUpdate bool
IsDelete bool
IsBatchCreate bool
IsBatchUpdate bool
IsBatchDelete bool
ResolveOrganizationID bool // TODO: something more pluggable
ResolveUserOrganizationID bool // TODO: something more pluggable
ResolveUserID bool // TODO: something more pluggable
Model Model
InputModel Model
BoilerWhiteList string
PublicErrorKey string
PublicErrorMessage string
}
func (rb *ResolverBuild) getResolverType(ty string) string {
for _, imp := range rb.Imports {
if strings.Contains(ty, imp.ImportPath) {
if imp.Alias != "" {
ty = strings.Replace(ty, imp.ImportPath, imp.Alias, -1)
} else {
ty = strings.Replace(ty, imp.ImportPath, "", -1)
}
}
}
return ty
}
func (rb *ResolverBuild) ShortResolverDeclaration(r *Resolver) string {
res := "(ctx context.Context"
if !r.Field.Object.Root {
res += fmt.Sprintf(", obj %s", rb.getResolverType(r.Field.Object.Reference().String()))
}
for _, arg := range r.Field.Args {
res += fmt.Sprintf(", %s %s", arg.VarName, rb.getResolverType(arg.TypeReference.GO.String()))
}
result := rb.getResolverType(r.Field.TypeReference.GO.String())
if r.Field.Object.Stream {
result = "<-chan " + result
}
res += fmt.Sprintf(") (%s, error)", result)
return res
}
func enhanceResolver(r *Resolver, models []*Model) { //nolint:gocyclo
nameOfResolver := r.Field.GoFieldName
// get model names + model convert information
modelName, inputModelName := getModelNames(nameOfResolver, false)
// modelPluralName, _ := getModelNames(nameOfResolver, true)
model := findModelOrEmpty(models, modelName)
inputModel := findModelOrEmpty(models, inputModelName)
// save for later inside file
r.Model = model
r.InputModel = inputModel
switch r.Object.Name {
case "Mutation":
r.IsCreate = containsPrefixAndPartAfterThatIsSingle(nameOfResolver, "Create")
r.IsUpdate = containsPrefixAndPartAfterThatIsSingle(nameOfResolver, "Update")
r.IsDelete = containsPrefixAndPartAfterThatIsSingle(nameOfResolver, "Delete")
r.IsBatchCreate = containsPrefixAndPartAfterThatIsPlural(nameOfResolver, "Create")
r.IsBatchUpdate = containsPrefixAndPartAfterThatIsPlural(nameOfResolver, "Update")
r.IsBatchDelete = containsPrefixAndPartAfterThatIsPlural(nameOfResolver, "Delete")
case "Query":
isPlural := IsPlural(nameOfResolver)
if isPlural {
r.IsList = isPlural
r.IsListBackward = strings.Contains(r.Field.GoFieldName, "first int") &&
strings.Contains(r.Field.GoFieldName, "after *string")
r.IsListBackward = strings.Contains(r.Field.GoFieldName, "last int") &&
strings.Contains(r.Field.GoFieldName, "before *string")
}
r.IsSingle = !r.IsList
case "Subscription":
// TODO: generate helpers for subscription
default:
log.Warn().Str("unknown", r.Object.Name).Msg(
"only Query and Mutation are handled we don't recognize the following")
}
lmName := strcase.ToLowerCamel(model.Name)
lmpName := strcase.ToLowerCamel(model.PluralName)
r.PublicErrorKey = "public"
if (r.IsCreate || r.IsDelete || r.IsUpdate) && strings.HasSuffix(lmName, "Batch") {
r.PublicErrorKey += "One"
}
r.PublicErrorKey += model.Name
switch {
case r.IsSingle:
r.PublicErrorKey += "Single"
r.PublicErrorMessage = "could not get " + lmName
case r.IsList:
r.PublicErrorKey += "List"
r.PublicErrorMessage = "could not list " + lmpName
case r.IsCreate:
r.PublicErrorKey += "Create"
r.PublicErrorMessage = "could not create " + lmName
case r.IsUpdate:
r.PublicErrorKey += "Update"
r.PublicErrorMessage = "could not update " + lmName
case r.IsDelete:
r.PublicErrorKey += "Delete"
r.PublicErrorMessage = "could not delete " + lmName
case r.IsBatchCreate:
r.PublicErrorKey += "BatchCreate"
r.PublicErrorMessage = "could not create " + lmpName
case r.IsBatchUpdate:
r.PublicErrorKey += "BatchUpdate"
r.PublicErrorMessage = "could not update " + lmpName
case r.IsBatchDelete:
r.PublicErrorKey += "BatchDelete"
r.PublicErrorMessage = "could not delete " + lmpName
}
r.PublicErrorKey += "Error"
}
func findModelOrEmpty(models []*Model, modelName string) Model {
if modelName == "" {
return Model{}
}
for _, m := range models {
if m.Name == modelName {
return *m
}
}
return Model{}
}
var InputTypes = []string{"Create", "Update", "Delete"} //nolint:gochecknoglobals
func getModelNames(v string, plural bool) (modelName, inputModelName string) {
var prefix string
var isInputType bool
for _, inputType := range InputTypes {
if strings.HasPrefix(v, inputType) {
isInputType = true
v = strings.TrimPrefix(v, inputType)
prefix = inputType
}
}
var s string
if plural {
s = Plural(v)
} else {
s = Singular(v)
}
if isInputType {
return s, s + prefix + "Input"
}
return s, ""
}
func containsPrefixAndPartAfterThatIsSingle(v string, prefix string) bool {
partAfterThat := strings.TrimPrefix(v, prefix)
return strings.HasPrefix(v, prefix) && IsSingular(partAfterThat)
}
func containsPrefixAndPartAfterThatIsPlural(v string, prefix string) bool {
partAfterThat := strings.TrimPrefix(v, prefix)
return strings.HasPrefix(v, prefix) && IsPlural(partAfterThat)
}