-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
272 lines (214 loc) · 6.37 KB
/
generate.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
package main
import (
"fmt"
"io"
"strings"
"text/template"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
_ "embed"
)
var typeMap = map[string]string{
"ID": "string",
"Int": "int64",
"uuid": "string",
"Float": "float64",
"Boolean": "bool",
"String": "string",
"_text": "[]string",
"timestamptz": "string",
"URL": "string",
}
//go:embed templates/schema.gotpl
var schemaTmpl string
//go:embed templates/inputs.gotpl
var inputsTmpl string
//go:embed templates/operations.gotpl
var operationsTmpl string
func generateInputs(schema *ast.Schema, out io.Writer) error {
fmt.Println("Generating input types...")
tmpl, err := template.New("inputs.gotpl").Funcs(template.FuncMap{
"formatName": formatName,
"formatScalar": formatScalar,
"formatType": formatType,
}).Parse(inputsTmpl)
err = tmpl.Execute(out, schema)
if err != nil {
return err
}
return nil
}
func generateSchema(schema *ast.Schema, out io.Writer) error {
fmt.Println("Generating schema types...")
tmpl, err := template.New("schema.gotpl").Funcs(template.FuncMap{
"formatName": formatName,
"formatScalar": formatScalar,
"formatType": formatType,
}).Parse(schemaTmpl)
err = tmpl.Execute(out, schema)
if err != nil {
return err
}
return nil
}
func generateOperations(schema *ast.Schema, queryDoc *ast.QueryDocument, out io.Writer) error {
fmt.Println("Generating operations...")
tmpl, err := template.New("operations.gotpl").Funcs(template.FuncMap{
"formatName": formatName,
"formatScalar": formatScalar,
"formatType": formatType,
"formatFragmentName": formatFragmentName,
"formatSelectionSet": formatSelectionSet,
"formatQuery": formatQuery,
}).Parse(operationsTmpl)
err = tmpl.Execute(out, queryDoc)
if err != nil {
return err
}
return nil
}
func parseQueryDocuments(schema *ast.Schema, documents []string) (*ast.QueryDocument, error) {
var parentDoc ast.QueryDocument
parentDoc.Operations = ast.OperationList{}
parentDoc.Fragments = ast.FragmentDefinitionList{}
for _, document := range documents {
queryDoc, err := gqlparser.LoadQuery(schema, document)
if err != nil {
return nil, err
}
operations := []*ast.OperationDefinition{}
for _, op := range queryDoc.Operations {
operations = append(operations, inlineOperationDefinition(queryDoc, op))
}
parentDoc.Operations = append(parentDoc.Operations, operations...)
parentDoc.Fragments = append(parentDoc.Fragments, queryDoc.Fragments...)
}
return &parentDoc, nil
}
func inlineOperationDefinition(queryDoc *ast.QueryDocument, operation *ast.OperationDefinition) *ast.OperationDefinition {
operation.SelectionSet = *inlineSelectionSet(queryDoc, &operation.SelectionSet)
return operation
}
func inlineSelectionSet(queryDoc *ast.QueryDocument, selectionSet *ast.SelectionSet) *ast.SelectionSet {
if selectionSet == nil || len(*selectionSet) == 0 {
return nil
}
inlined := ast.SelectionSet{}
for _, selection := range *selectionSet {
switch selection := selection.(type) {
case *ast.Field:
if len(selection.SelectionSet) > 0 {
selection.SelectionSet = *inlineSelectionSet(queryDoc, &selection.SelectionSet)
inlined = append(inlined, selection)
} else {
inlined = append(inlined, selection)
}
case *ast.FragmentSpread:
inlined = append(inlined, *inlineSelectionSet(queryDoc, &selection.Definition.SelectionSet)...)
case *ast.InlineFragment:
inlined = append(inlined, *inlineSelectionSet(queryDoc, &selection.SelectionSet)...)
default:
}
}
return &inlined
}
func snakeToCamel(s string) string {
tokens := strings.Split(s, "_")
var sb strings.Builder
for _, token := range tokens {
sb.WriteString(strings.Title(token))
}
return sb.String()
}
func formatName(name string) string {
return snakeToCamel(name)
}
func formatFragmentName(name string) string {
return strings.Title(name) + "Fragment"
}
func formatScalar(scalar string) string {
newType, ok := typeMap[scalar]
if ok {
return newType
} else {
return "string"
}
}
func formatType(t *ast.Type) string {
var sb strings.Builder
if !t.NonNull {
sb.WriteString("*")
}
if t.Elem != nil {
newType, ok := typeMap[t.Elem.NamedType]
if ok {
sb.WriteString("[]" + newType)
} else {
sb.WriteString("[]" + snakeToCamel(t.Elem.NamedType))
}
} else {
newType, ok := typeMap[t.NamedType]
if ok {
sb.WriteString(newType)
} else {
sb.WriteString(snakeToCamel(t.NamedType))
}
}
return sb.String()
}
func formatSelectionSet(selectionSet ast.SelectionSet, depth int) string {
if len(selectionSet) == 0 { return "" }
var sb strings.Builder
// this recursion feels overcomplicated
for _, selection := range selectionSet {
switch selection := selection.(type) {
case *ast.Field:
for i := 0; i <= depth; i++ {
sb.WriteString(" ")
}
// FIXME: Does not take aliases into account
if len(selection.SelectionSet) == 0 {
sb.WriteString(
strings.Title(selection.Name) + " " + formatType(selection.Definition.Type) + " `json:\"" + selection.Name + "\"`\n",
)
} else {
sb.WriteString(strings.Title(selection.Name) + " ")
if !selection.Definition.Type.NonNull {
sb.WriteString("*")
}
if selection.Definition.Type.Elem != nil {
sb.WriteString("[]")
}
sb.WriteString(
"struct {\n" + formatSelectionSet(selection.SelectionSet, depth + 1),
)
for i := 0; i <= depth; i++ {
sb.WriteString(" ")
}
sb.WriteString(
"} `json:\"" + selection.Name + "\"`\n",
)
}
case *ast.FragmentSpread:
if len(selection.Definition.SelectionSet) > 0 {
sb.WriteString(
formatSelectionSet(selection.Definition.SelectionSet, depth),
)
}
case *ast.InlineFragment:
if len(selection.SelectionSet) > 0 {
sb.WriteString(
formatSelectionSet(selection.SelectionSet, depth),
)
}
default:
}
}
return sb.String()
}
func formatQuery(op *ast.OperationDefinition, fragments ast.FragmentDefinitionList) string {
var sb strings.Builder
f := Formatter{Writer: &sb}
f.FormatOperationDefinition(op)
return sb.String()
}