-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema_gql.go
347 lines (299 loc) · 7.12 KB
/
schema_gql.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
package gontentful
import (
"bytes"
"fmt"
"strings"
"text/template"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const gqlTemplate = `# Code generated, DO NOT EDIT
schema {
query: Query
}
type Query {
{{- range $_ := .TypeDefs }}
{{- range $_ := .Resolvers }}
{{ .Name }}(
{{- range $i, $_ := .Args -}}
{{ if $i }}, {{ end }}{{ .ArgName }}: {{ .ArgType }}
{{- end }}): {{ .Result }}
{{- end }}
{{- end }}
}
scalar Map
interface Sys {
id: ID!
createdAt: String!
updatedAt: String!
}
interface Entry {
sys: EntrySys!
}
type EntrySys implements Sys {
id: ID!
createdAt: String!
updatedAt: String!
contentTypeId: ID!
}
{{- range $t := .TypeDefs }}
{{ if $t }}{{ end }}
type {{ .TypeName }} implements Entry {
sys: EntrySys!
{{- range $_ := .Fields }}
{{ .FieldName }}: {{ .FieldType }}
{{- end }}
}
{{- end }}
type FileDetailsImage {
height: Int
width: Int
}
type FileDetails {
size: Int
image: FileDetailsImage
}
type File {
contentType: String
fileName: String
url: String
details: FileDetails
}
type AssetSys implements Sys {
id: ID!
createdAt: String!
updatedAt: String!
}
type Asset {
sys: AssetSys!
title: String
description: String
url: String
file: File
}`
var (
singleArgs = []*GraphQLResolverArg{
&GraphQLResolverArg{"idArg", "ID"},
&GraphQLResolverArg{"localeArg", "String"},
&GraphQLResolverArg{"includeArg", "Int"},
&GraphQLResolverArg{"selectArg", "String"},
}
singleIdentityFields = []*GraphQLResolverArg{
&GraphQLResolverArg{"slug", "String"},
&GraphQLResolverArg{"code", "String"},
&GraphQLResolverArg{"name", "String"},
&GraphQLResolverArg{"key", "String"},
&GraphQLResolverArg{"level", "Int"},
}
collectionArgs = []*GraphQLResolverArg{
&GraphQLResolverArg{"localeArg", "String"},
&GraphQLResolverArg{"skipArg", "Int"},
&GraphQLResolverArg{"limitArg", "Int"},
&GraphQLResolverArg{"includeArg", "Int"},
&GraphQLResolverArg{"selectArg", "String"},
&GraphQLResolverArg{"orderArg", "String"},
&GraphQLResolverArg{"qArg", "String"},
}
)
type GraphQLResolver struct {
Name string
Args []*GraphQLResolverArg
Result string
}
type GraphQLResolverArg struct {
ArgName string
ArgType string
}
type GraphQLField struct {
FieldName string
FieldType string
}
type GraphQLType struct {
Schema GraphQLSchema
TypeName string
Fields []*GraphQLField
Resolvers []*GraphQLResolver
}
type GraphQLSchema struct {
Items []*ContentType
TypeDefs []*GraphQLType
}
func NewGraphQLSchema(items []*ContentType) *GraphQLSchema {
schema := &GraphQLSchema{
Items: items,
TypeDefs: make([]*GraphQLType, 0),
}
for _, item := range items {
typeDef := NewGraphQLTypeDef(schema, item.Sys.ID, item.Fields)
schema.TypeDefs = append(schema.TypeDefs, typeDef)
}
return schema
}
func (s *GraphQLSchema) Render() (string, error) {
tmpl, err := template.New("").Parse(gqlTemplate)
if err != nil {
return "", err
}
var buff bytes.Buffer
err = tmpl.Execute(&buff, s)
if err != nil {
return "", err
}
return buff.String(), nil
}
func NewGraphQLTypeDef(schema *GraphQLSchema, typeName string, fields []*ContentTypeField) *GraphQLType {
typeDef := &GraphQLType{
TypeName: cases.Title(language.Und, cases.NoLower).String(typeName),
Fields: make([]*GraphQLField, 0),
Resolvers: make([]*GraphQLResolver, 0),
}
// single
typeDef.Resolvers = append(typeDef.Resolvers, NewGraphQLResolver(false, typeName, getResolverArgs(false, fields), typeDef.TypeName))
// collection
typeDef.Resolvers = append(typeDef.Resolvers, NewGraphQLResolver(true, pluralName(typeName), getResolverArgs(true, fields), typeDef.TypeName))
for _, f := range fields {
if f.Disabled || f.Omitted {
continue
}
field := NewGraphQLField(schema, f)
typeDef.Fields = append(typeDef.Fields, field)
}
return typeDef
}
func NewGraphQLResolver(collection bool, name string, args []*GraphQLResolverArg, result string) *GraphQLResolver {
if collection {
result = fmt.Sprintf("[%s]", result)
}
return &GraphQLResolver{
Name: name,
Args: args,
Result: result,
}
}
func getResolverArgs(collection bool, fields []*ContentTypeField) []*GraphQLResolverArg {
if collection {
return getCollectionArgs(fields)
}
return getSingleArgs(fields)
}
func getSingleArgs(fields []*ContentTypeField) []*GraphQLResolverArg {
args := singleArgs
for _, a := range singleIdentityFields {
if hasField(fields, a.ArgName) {
args = append(args, a)
}
}
return args
}
func getCollectionArgs(fields []*ContentTypeField) []*GraphQLResolverArg {
args := collectionArgs
for _, f := range fields {
t := isOwnField(f)
if len(t) > 0 {
args = append(args, &GraphQLResolverArg{
ArgName: f.ID,
ArgType: t,
})
}
}
return args
}
func isOwnField(field *ContentTypeField) string {
if field.Type == LINK || field.Type == ARRAY {
return ""
}
switch field.Type {
case "Integer":
return "Int"
case "Number":
return "Float"
case "Boolean":
return "Boolean"
default:
return "String"
}
}
func hasField(fields []*ContentTypeField, id string) bool {
for _, f := range fields {
if f.ID == id {
return true
}
}
return false
}
func NewGraphQLField(schema *GraphQLSchema, f *ContentTypeField) *GraphQLField {
return &GraphQLField{
FieldName: f.ID,
FieldType: isRequired(f.Required, getFieldType(schema, f)),
}
}
func isRequired(r bool, s string) string {
if r {
s += "!"
}
return s
}
func getFieldType(schema *GraphQLSchema, field *ContentTypeField) string {
switch field.Type {
case "Symbol":
return "String"
case "Text":
return "String"
case "Integer":
return "Int"
case "Number":
return "Float"
case "Date":
return "String"
case "Location":
return "String"
case "Boolean":
return "Boolean"
case "Array":
return getArrayType(schema, field)
case "Link":
return getLinkType(schema, field)
case "Object":
return "Map" // scalar Map
default:
return "String"
}
}
func getArrayType(schema *GraphQLSchema, field *ContentTypeField) string {
if field.Items == nil || len(field.Items.LinkType) == 0 {
return "[String]"
}
return fmt.Sprintf("[%s]", getValidationContentType(schema, field.Items.LinkType, field.Items.Validations))
}
func getLinkType(schema *GraphQLSchema, field *ContentTypeField) string {
return getValidationContentType(schema, field.LinkType, field.Validations)
}
func getValidationContentType(schema *GraphQLSchema, t string, validations []*FieldValidation) string {
if len(validations) > 0 && len(validations[0].LinkContentType) > 0 {
vt := validations[0].LinkContentType[0]
// check if validation content type exists
for _, item := range schema.Items {
if item.Sys.ID == vt {
t = vt
break
}
}
}
return cases.Title(language.Und, cases.NoLower).String(t)
}
func pluralName(typeName string) string {
// s -> ses
// y -> ies
// o -> oes
lastChar := typeName[len(typeName)-1:]
if lastChar == "s" {
typeName = strings.TrimSuffix(typeName, lastChar) + "ses"
} else if lastChar == "y" {
typeName = strings.TrimSuffix(typeName, lastChar) + "ies"
} else if lastChar == "o" {
typeName = strings.TrimSuffix(typeName, lastChar) + "oes"
} else {
typeName += "s"
}
return typeName
}