Skip to content

Commit c373520

Browse files
committed
Add ability to set GraphQL struct name for the returned object
1 parent 795ca47 commit c373520

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

internal/result.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
186186
comments := query.Comments
187187
var extendedType string
188188
resolverName := query.Name
189+
returnType := ""
189190
for i, comment := range comments {
190191
parts := strings.Split(comment, ":")
191192
if len(parts) > 1 && strings.Trim(parts[0], " ") == "gql" {
@@ -195,6 +196,9 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
195196
if len(resolverInfo) > 1 {
196197
resolverName = resolverInfo[1]
197198
}
199+
if len(parts) > 2 {
200+
returnType = strings.Trim(parts[2], " ")
201+
}
198202
comments = append(comments[:i], comments[i+1:]...)
199203
break
200204
}
@@ -228,6 +232,10 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
228232
CursorPagination: cursorPagination,
229233
}
230234

235+
if returnType == "" {
236+
returnType = gq.MethodName + "Row"
237+
}
238+
231239
qpl := int(*options.QueryParameterLimit)
232240

233241
if paginated {
@@ -366,13 +374,15 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
366374
)
367375
}
368376
var err error
369-
gs, err = columnsToStruct(req, options, gq.MethodName+"Row", columns, true)
377+
gs, err = columnsToStruct(req, options, returnType, columns, true)
370378
if err != nil {
371379
return nil, err
372380
}
373381
emit = true
374382
modelPath = options.Package + "." + gq.MethodName + "Row"
375383
}
384+
gs.ModelPath = modelPath
385+
376386
gq.Ret = QueryValue{
377387
Emit: emit,
378388
Name: "i",

internal/struct.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
)
1111

1212
type Struct struct {
13-
Table *plugin.Identifier
14-
Name string
15-
Fields []Field
16-
Comment string
13+
Table *plugin.Identifier
14+
Name string
15+
Fields []Field
16+
Comment string
17+
ModelPath string
1718
}
1819

1920
func StructName(name string, options *opts.Options) string {

internal/templates/schema.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum {{.Name}} @goModel(model: "{{$.ModelPackage}}.{{.Name}}") {
2828
{{ .Comment}}
2929
"""
3030
{{- end }}
31-
type {{.Name}} @goModel(model: "{{$.ModelPackage}}.{{.Name}}") {
31+
type {{.Name}} @goModel(model: "{{.ModelPath}}") {
3232
{{- range .Fields -}}
3333
{{ if .Comment }}
3434
"""

0 commit comments

Comments
 (0)