-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alumbra.cljc
526 lines (473 loc) · 17.6 KB
/
alumbra.cljc
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
(ns alumbra.js.ast.alumbra
"Transform a GraphQL AST from Alumbra to JS."
(:require [clojure.string :as str]))
(declare type->js selection-set->js)
(defn- meatadata->loc
"Convert Alumbra metadata to a JS location."
[{:keys [column index row]}]
#?(:clj {:startToken {:column column :line row :start index}}
:cljs #js {:startToken #js {:column column :line row :start index}}))
(defn- name->js [v metadata]
(when v
{:kind "Name"
:loc (meatadata->loc metadata)
:value v}))
(defmulti value->js
"Transform a GraphQL value from Alumbra to JS."
:alumbra/value-type)
(defmethod value->js :boolean
[{:keys [alumbra/boolean alumbra/metadata]}]
{:kind "BooleanValue"
:loc (meatadata->loc metadata)
:value boolean})
(defmethod value->js :enum
[{:keys [alumbra/enum alumbra/metadata]}]
{:kind "EnumValue"
:loc (meatadata->loc metadata)
:value enum})
(defmethod value->js :float
[{:keys [alumbra/float alumbra/metadata]}]
{:kind "FloatValue"
:loc (meatadata->loc metadata)
:value (str float)})
(defmethod value->js :integer
[{:keys [alumbra/integer alumbra/metadata]}]
{:kind "IntValue"
:loc (meatadata->loc metadata)
:value (str integer)})
(defmethod value->js :list
[{:keys [alumbra/list alumbra/metadata]}]
{:kind "ListValue"
:loc (meatadata->loc metadata)
:values (mapv value->js list)})
(defmethod value->js :null
[{:keys [alumbra/metadata]}]
{:kind "NullValue"
:loc (meatadata->loc metadata)})
(defmethod value->js :string
[{:keys [alumbra/metadata alumbra/string]}]
{:block false ;; TODO: What is block?
:kind "StringValue"
:loc (meatadata->loc metadata)
:value string})
(defn- object-field->js
"Transform a GraphQL object field from Alumbra to JS."
[{:keys [alumbra/field-name alumbra/metadata alumbra/value]}]
{:kind "ObjectField"
:loc (meatadata->loc metadata)
:name (name->js field-name metadata)
:value (value->js value)})
(defmethod value->js :object
[{:keys [alumbra/metadata alumbra/object]}]
{:kind "ObjectValue"
:loc (meatadata->loc metadata)
:fields (mapv object-field->js object)})
(defmethod value->js :variable
[{:keys [alumbra/metadata alumbra/variable-name]}]
{:kind "Variable"
:loc (meatadata->loc metadata)
:name (name->js variable-name metadata)})
(defn- argument->js
"Transform a GraphQL argument from Alumbra to JS."
[{:keys [alumbra/argument-name alumbra/argument-value alumbra/metadata]}]
{:kind "Argument"
:loc (meatadata->loc metadata)
:name (name->js argument-name metadata)
:value (value->js argument-value)})
(declare directive->js)
(defn- argument-definition->js
"Transform a GraphQL argument definition from Alumbra to JS."
[{:keys [alumbra/argument-name
alumbra/argument-type
alumbra/directives
alumbra/default-value
alumbra/metadata]}]
{:defaultValue (some-> default-value value->js)
:description nil
:directives (mapv directive->js directives)
:kind "InputValueDefinition"
:loc (meatadata->loc metadata)
:name (name->js argument-name metadata)
:type (type->js argument-type)})
(defn- directive->js
"Transform a GraphQL directive from Alumbra to JS."
[{:keys [alumbra/arguments alumbra/metadata alumbra/directive-name]}]
{:arguments (mapv argument->js arguments)
:kind "Directive"
:loc (meatadata->loc metadata)
:name (name->js directive-name metadata)})
(defn- named-type->js
"Transform a GraphQL type condition from Alumbra to JS."
[{:keys [alumbra/metadata alumbra/type-name]}]
{:kind "NamedType"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- fragment->js
"Transform a GraphQL fragment from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/fragment-name
alumbra/metadata
alumbra/type-condition
alumbra/selection-set]}]
{:kind "FragmentDefinition"
:name (name->js fragment-name metadata)
:loc (meatadata->loc metadata)
:typeCondition (named-type->js type-condition)
:directives (mapv directive->js directives)
:selectionSet (selection-set->js selection-set)})
(defn- fragment-spread->js
"Transform a GraphQL fragment spread from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/fragment-name
alumbra/metadata]}]
{:directives (mapv directive->js directives)
:kind "FragmentSpread"
:loc (meatadata->loc metadata)
:name (name->js fragment-name metadata)})
(defn- field->js
"Transform a GraphQL field from Alumbra to JS."
[{:keys [alumbra/arguments
alumbra/directives
alumbra/field-alias
alumbra/field-name
alumbra/metadata
alumbra/selection-set]}]
{:alias (name->js field-alias metadata)
:arguments (mapv argument->js arguments)
:directives (mapv directive->js directives)
:kind "Field"
:loc (meatadata->loc metadata)
:name (name->js field-name metadata)
:selectionSet (selection-set->js selection-set)})
(defn- field-definition->js
"Transform a GraphQL field definition from Alumbra to JS."
[{:keys [alumbra/arguments
alumbra/argument-definitions
alumbra/directives
alumbra/field-name
alumbra/metadata
alumbra/type]}]
{:arguments
(cond
(seq arguments)
(mapv argument->js arguments)
(seq argument-definitions)
(mapv argument-definition->js argument-definitions)
:else [])
:kind "FieldDefinition"
:loc (meatadata->loc metadata)
:description nil
:name (name->js field-name metadata)
:type (type->js type)
:directives (mapv directive->js directives)})
(defn- inline-fragment->js
"Transform a GraphQL inline fragment from Alumbra to JS."
[{:keys [alumbra/arguments
alumbra/directives
alumbra/field-name
alumbra/metadata
alumbra/selection-set
alumbra/type-condition]}]
{:directives (mapv directive->js directives)
:kind "InlineFragment"
:loc (meatadata->loc metadata)
:selectionSet (selection-set->js selection-set)
:typeCondition (some-> type-condition named-type->js)})
(defn- input-field-definition->js
"Transform a GraphQL input field definition from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/default-value
alumbra/field-name
alumbra/metadata
alumbra/type]}]
{:kind "InputValueDefinition"
:loc (meatadata->loc metadata)
:description nil
:name (name->js field-name metadata)
:type (type->js type)
:defaultValue (some-> default-value value->js)
:directives (mapv directive->js directives)})
(defn- input-type-definition->js
"Transform a GraphQL input type definition from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/input-field-definitions
alumbra/metadata
alumbra/type-name]}]
{:description nil
:directives (mapv directive->js directives)
:fields (mapv input-field-definition->js input-field-definitions)
:kind "InputObjectTypeDefinition"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- input-extension->js
"Transform a GraphQL input extension from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/input-field-definitions
alumbra/metadata
alumbra/type-name]}]
{:directives (mapv directive->js directives)
:fields (mapv input-field-definition->js input-field-definitions)
:kind "InputObjectTypeExtension"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- interface-definition->js
"Transform a GraphQL interface definition from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/field-definitions
alumbra/metadata
alumbra/type-name]}]
{:description nil
:directives (mapv directive->js directives)
:fields (mapv field-definition->js field-definitions)
:kind "InterfaceTypeDefinition"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- interface-extension->js
"Transform a GraphQL interface extension from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/field-definitions
alumbra/metadata
alumbra/type-name]}]
{:kind "InterfaceTypeExtension"
:loc (meatadata->loc metadata)
:directives (mapv directive->js directives)
:fields (mapv field-definition->js field-definitions)
:name (name->js type-name metadata)})
(defn- type-definition->js
"Transform a GraphQL type definition from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/field-definitions
alumbra/interface-types
alumbra/metadata
alumbra/type-name]}]
{:description nil
:directives (mapv directive->js directives)
:fields (mapv field-definition->js field-definitions)
:interfaces (mapv named-type->js interface-types)
:kind "ObjectTypeDefinition"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- type-extension->js
"Transform a GraphQL type extension from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/field-definitions
alumbra/interface-types
alumbra/metadata
alumbra/type-name]}]
{:kind "ObjectTypeExtension"
:loc (meatadata->loc metadata)
:directives (mapv directive->js directives)
:fields (mapv field-definition->js field-definitions)
:interfaces (mapv named-type->js interface-types)
:name (name->js type-name metadata)})
(defn- selection->js
"Transform a GraphQL selection from Alumbra to JS."
[{:keys [alumbra/field-name
alumbra/fragment-name
alumbra/selection-set
alumbra/type-condition]
:as selection}]
(cond
field-name
(field->js selection)
fragment-name
(fragment-spread->js selection)
type-condition
(inline-fragment->js selection)
selection-set
(inline-fragment->js selection)))
(defn- selection-set->js
"Transform a GraphQL selection set from Alumbra to JS."
[selections]
(when (seq selections)
{:kind "SelectionSet"
;; TODO:
;; :loc (meatadata->loc metadata)
:selections (mapv selection->js selections)}))
(defmulti type-class->js
"Transform a GraphQL type from Alumbra to JS."
:alumbra/type-class)
(defmethod type-class->js :named-type
[{:keys [alumbra/metadata alumbra/type-name]}]
{:kind "NamedType"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defmethod type-class->js :list-type
[{:keys [alumbra/element-type alumbra/metadata]}]
{:kind "ListType"
:loc (meatadata->loc metadata)
:type (type->js element-type)})
(defn- type-non-null->js [type]
(let [{:keys [alumbra/metadata alumbra/non-null?]} type]
(assert non-null?)
{:kind "NonNullType"
:loc (meatadata->loc metadata)
:type (type-class->js type)}))
(defn- type->js [type]
(if (:alumbra/non-null? type)
(type-non-null->js type)
(type-class->js type)))
(defn- variable->js
"Transform a GraphQL variable from Alumbra to JS."
[{:keys [alumbra/default-value
alumbra/metadata
alumbra/type
alumbra/variable-name]}]
{:defaultValue (some-> default-value value->js)
:kind "VariableDefinition"
:loc (meatadata->loc metadata)
:type (type->js type)
:variable
{:kind "Variable"
:name (name->js variable-name metadata)}})
(defn- operation->js
"Transform a GraphQL operation from Alumbra to JS."
[{:keys [alumbra/directives
alumbra/fragments
alumbra/metadata
alumbra/operation-type
alumbra/operation-name
alumbra/selection-set
alumbra/variables]}]
{:directives (mapv directive->js directives)
:kind "OperationDefinition"
:loc (meatadata->loc metadata)
:name (name->js operation-name metadata)
:operation operation-type
:selectionSet (selection-set->js selection-set)
:variableDefinitions (mapv variable->js variables)})
(defn- operation-definition->js
"Transform a GraphQL operation type definition from Alumbra to JS."
[{:keys [alumbra/metadata alumbra/operation-type alumbra/schema-type]}]
{:kind "OperationTypeDefinition"
:loc (meatadata->loc metadata)
:operation operation-type
:type (named-type->js schema-type)})
(defn- schema-definition->js
"Transform a GraphQL schema definition from Alumbra to JS."
[{:keys [alumbra/directives alumbra/metadata alumbra/schema-fields]}]
{:kind "SchemaDefinition"
:loc (meatadata->loc metadata)
:directives (mapv directive->js directives)
:operationTypes (mapv operation-definition->js schema-fields)})
(defn- schema-extension->js
"Transform a GraphQL schema extension from Alumbra to JS."
[{:keys [alumbra/directives alumbra/metadata alumbra/schema-fields]}]
{:kind "SchemaExtension"
:loc (meatadata->loc metadata)
:directives (mapv directive->js directives)
:operationTypes (mapv operation-definition->js schema-fields)})
(defn- enum-value-definition->js
"Transform a GraphQL enum value from Alumbra to JS."
[{:keys [alumbra/directives alumbra/enum alumbra/metadata]}]
{:description nil
:directives (mapv directive->js directives)
:kind "EnumValueDefinition"
:loc (meatadata->loc metadata)
:name (name->js enum metadata)})
(defn- enum-definition->js
"Transform a GraphQL enum definition from Alumbra to JS."
[{:keys [alumbra/directives alumbra/enum-fields alumbra/metadata alumbra/type-name]}]
{:description nil
:directives (mapv directive->js directives)
:kind "EnumTypeDefinition"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)
:values (mapv enum-value-definition->js enum-fields)})
(defn- enum-extension->js
"Transform a GraphQL enum extension from Alumbra to JS."
[{:keys [alumbra/directives alumbra/enum-fields alumbra/metadata alumbra/type-name]}]
{:directives (mapv directive->js directives)
:kind "EnumTypeExtension"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)
:values (mapv enum-value-definition->js enum-fields)})
(defn- directive-location->js
"Transform a GraphQL directive definition from Alumbra to JS."
[location metadata]
(-> location name str/upper-case (str/replace "-" "_")
(name->js metadata)))
(defn- directive-definition->js
"Transform a GraphQL directive definition from Alumbra to JS."
[{:keys [alumbra/argument-definitions
alumbra/directive-name
alumbra/directive-locations
alumbra/metadata]}]
{:arguments (mapv argument-definition->js argument-definitions)
:description nil
:kind "DirectiveDefinition"
:loc (meatadata->loc metadata)
:locations (mapv #(directive-location->js % metadata) directive-locations)
:name (name->js directive-name metadata)})
(defn- scalar-definition->js
"Transform a GraphQL scalar definition from Alumbra to JS."
[{:keys [alumbra/directives alumbra/metadata alumbra/type-name]}]
{:description nil
:directives (mapv directive->js directives)
:kind "ScalarTypeDefinition"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- scalar-extension->js
"Transform a GraphQL scalar extension from Alumbra to JS."
[{:keys [alumbra/directives alumbra/metadata alumbra/type-name]}]
{:directives (mapv directive->js directives)
:kind "ScalarTypeExtension"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)})
(defn- union-definition->js
"Transform a GraphQL union definition from Alumbra to JS."
[{:keys [alumbra/directives alumbra/metadata alumbra/type-name alumbra/union-types]}]
{:description nil
:directives (mapv directive->js directives)
:kind "UnionTypeDefinition"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)
:types (mapv named-type->js union-types)})
(defn- union-extension->js
"Transform a GraphQL union extension from Alumbra to JS."
[{:keys [alumbra/directives alumbra/metadata alumbra/type-name alumbra/union-types]}]
{:directives (mapv directive->js directives)
:kind "UnionTypeExtension"
:loc (meatadata->loc metadata)
:name (name->js type-name metadata)
:types (mapv named-type->js union-types)})
(defn alumbra->js
"Transform a GraphQL document from Alumbra to JS."
[{:keys [alumbra/enum-definitions
alumbra/directive-definitions
alumbra/enum-extensions
alumbra/fragments
alumbra/input-extensions
alumbra/input-type-definitions
alumbra/interface-definitions
alumbra/interface-extensions
alumbra/metadata
alumbra/operations
alumbra/scalar-definitions
alumbra/scalar-extensions
alumbra/schema-definitions
alumbra/schema-extensions
alumbra/type-definitions
alumbra/type-extensions
alumbra/union-definitions
alumbra/union-extensions]}]
{:definitions
(vec (concat (map schema-definition->js schema-definitions)
(map schema-extension->js schema-extensions)
(map scalar-definition->js scalar-definitions)
(map scalar-extension->js scalar-extensions)
(map enum-definition->js enum-definitions)
(map enum-extension->js enum-extensions)
(map directive-definition->js directive-definitions)
(map input-type-definition->js input-type-definitions)
(map input-extension->js input-extensions)
(map interface-definition->js interface-definitions)
(map interface-extension->js interface-extensions)
(map type-definition->js type-definitions)
(map type-extension->js type-extensions)
(map union-definition->js union-definitions)
(map union-extension->js union-extensions)
(map operation->js operations)
(map fragment->js fragments)))
:kind "Document"
:loc (meatadata->loc metadata)})