-
Notifications
You must be signed in to change notification settings - Fork 161
/
schema.clj
563 lines (481 loc) · 18.1 KB
/
schema.clj
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
; Copyright (c) 2017-present Walmart, Inc.
;
; Licensed under the Apache License, Version 2.0 (the "License")
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
(ns com.walmartlabs.lacinia.parser.schema
"Parse a Schema Definition Language document into a Lacinia input schema."
{:added "0.22.0"}
(:require
#_ [io.pedestal.log :as log]
[com.walmartlabs.lacinia.internal-utils :refer [remove-vals keepv q qualified-name]]
[com.walmartlabs.lacinia.parser.common :as common]
[com.walmartlabs.lacinia.util :refer [inject-descriptions]]
[com.walmartlabs.lacinia.schema :as schema]
[clojure.spec.alpha :as s]
[clojure.string :as str])
(:import
(clj_antlr ParseError)))
;; When using Clojure 1.8, the dependency on clojure-future-spec must be included,
;; and this code will trigger
(when (-> *clojure-version* :minor (< 9))
(require '[clojure.future :refer [simple-keyword?]]))
(def ^:private grammar
(common/compile-grammar "com/walmartlabs/lacinia/schema.g4"))
(defn ^:private tag
"Returns a map of the nested productions in a production.
Nested productions are elements after the first; the key is the
first element in each nested production.
Note that this should only be used in cases where none of the productions repeat."
[prod]
(reduce (fn [m p]
(assoc m (first p) p))
{}
(rest prod)))
(defn is-extension?
[v]
(get (meta v) :extension false))
(defn ^:private merge-type-extension
[k v org]
(doseq [property (keys (get org :fields {}))]
(when (contains? (get v :fields {}) property)
(let [locations (keepv meta [org v])]
(throw (ex-info (format "Field %s already defined in the existing schema. It cannot also be defined in this type extension."
(q (qualified-name k property)))
(cond-> {:key k}
(seq locations) (assoc :locations locations)))))))
(reduce merge
{}
[{:fields (merge (get org :fields) (get v :fields))}
(some->> (into (get org :implements []) (get v :implements []))
(distinct)
(vec)
(#(if (not-empty %) % nil))
(assoc {} :implements))
; TODO check and/or remove duplicate directives
(some->> (into (get org :directives []) (get v :directives []))
(#(if (not-empty %) % nil))
(assoc {} :directives))
(some->> [(get org :description) (get v :description)]
(remove nil?)
(str/join " ")
(#(if (not-empty %) % nil))
(assoc {} :description))]))
(defn ^:private assoc-check
[m content k v]
(cond (and (not (contains? m k))
(is-extension? v))
(let [locations (keepv meta [(get m k) v])]
(throw (ex-info (format "Cannot extend type %s because it does not exist in the existing schema."
(q k))
(cond-> {:key k}
(seq locations) (assoc :locations locations)))))
(not (contains? m k))
(assoc m k v)
(is-extension? v)
(update m k (partial merge-type-extension k v))
(contains? m k)
(let [locations (keepv meta [(get m k) v])]
(throw (ex-info (format "Conflicting %s: %s."
content
(q k))
(cond-> {:key k}
(seq locations) (assoc :locations locations)))))))
(defn ^:private checked-map
"Given a seq of key/value tuples, assembles a map, checking that keys do not conflict
(throwing an exception if they do).
content describes what is being built, and is used for exception messages."
[content kvs]
(reduce (fn [m [k v]]
(assoc-check m content k v))
{}
kvs))
(defn ^:private attach-field-fns
"Attaches a map of either resolvers or subscription streamers"
[schema fn-k fn-map]
{:pre [(#{:resolve :stream} fn-k)]}
(reduce-kv (fn [schema' type fields]
(reduce-kv (fn [schema'' field f]
(assoc-in schema'' [:objects type :fields field fn-k] f))
schema'
fields))
schema
fn-map))
(defn ^:private attach-scalars
[schema scalars]
(if (seq scalars)
(update schema :scalars
(fn [s]
(reduce-kv (fn [s' scalar-name scalar-value]
(update s' scalar-name merge scalar-value))
s
scalars)))
schema))
;; This is very similar to the code for parsing a query, and includes a bit of duplication.
;; Perhaps at some point we can merge it all into a single, unified grammar.
(defmulti ^:private xform
"Transform an Antlr production into a result.
Antlr productions are recursive lists; the first element is a type
(from the grammar), and the rest of the list are nested productions.
Meta data on the production is the location (line, column) of the production."
first
;; When debugging/developing, this is incredibly useful:
#_(fn [prod]
(log/trace :dispatch prod)
(first prod))
:default ::default)
(defn ^:private xform-second
[prod]
(-> prod second xform))
(defn ^:private apply-description
[parsed descripion-prod]
(cond-> parsed
descripion-prod (assoc :description (xform descripion-prod))))
(defn ^:private apply-directives
[element directiveList]
(if directiveList
(assoc element :directives (xform directiveList))
element))
(defmethod xform :schemaDef
[prod]
(let [sub-prods (drop 2 prod)
directiveList (when (= :directiveList (-> sub-prods first first))
(first sub-prods))
remaining-prods (if directiveList
(rest sub-prods)
sub-prods)]
[[:roots] (-> (checked-map "schema entry" (map xform remaining-prods))
;; Temporary location until patch-schema-directives is invoked
(apply-directives directiveList))]))
(defn ^:private patch-schema-directives
[schema]
(if-let [directives (get-in schema [:roots :directives])]
(-> schema
(assoc :directives directives)
(update :roots dissoc :directives))
schema))
(defmethod xform :operationTypeDef
[prod]
(xform (second prod)))
(defmethod xform :queryOperationDef
[prod]
(let [[_ _ type-prod] prod]
[:query (xform type-prod)]))
(defmethod xform :mutationOperationDef
[prod]
(let [[_ _ type-prod] prod]
[:mutation (xform type-prod)]))
(defmethod xform :subscriptionOperationDef
[prod]
(let [[_ _ type-prod] prod]
[:subscription (xform type-prod)]))
(defmethod xform :typeName
[prod]
(let [name-k (xform-second prod)]
;; By convention, these type names for built-in types are represented as
;; symbols, not keywords.
(if (#{:Boolean :String :Int :Float :ID} name-k)
(-> name-k name symbol)
name-k)))
(defmethod xform :name
[prod]
(-> prod second keyword))
(defmethod xform :anyName
[prod]
(xform-second prod))
(defmethod xform :nameTokens
[prod]
(xform (list :name (-> prod second second))))
(defmethod xform :description
[prod]
(xform-second prod))
(defmethod xform :typeDef
[prod]
(let [{:keys [anyName implementationDef fieldDefs description directiveList]
:or {fieldDefs (list :fieldDefs)}} (tag prod)]
[[:objects (xform anyName)]
(-> {:fields (xform fieldDefs)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList)
(cond-> implementationDef (assoc :implements (xform implementationDef))))]))
(defmethod xform :typeExtDef
[prod]
(let [{:keys [anyName implementationDef fieldDefs description directiveList]
:or {fieldDefs (list :fieldDefs)}} (tag prod)]
(with-meta [[:objects (xform anyName)]
(-> {:fields (xform fieldDefs)}
(common/copy-meta anyName)
(common/add-meta {:extension true})
(apply-description description)
(apply-directives directiveList)
(cond-> implementationDef (assoc :implements (xform implementationDef))))]
{:extension true})))
(defmethod xform :directiveDef
[prod]
(let [{:keys [anyName argList directiveLocationList description]} (tag prod)]
[[:directive-defs (xform anyName)]
(-> {:locations (xform directiveLocationList)}
(common/copy-meta anyName)
(apply-description description)
(cond->
argList (assoc :args (xform argList))))]))
(defmethod xform :directiveLocationList
[prod]
(->> prod
rest
(filter #(-> % first (= :directiveLocation)))
(map xform)
set))
(defn ^:private directive-name->keyword
[s]
(-> s
str/lower-case
(str/replace "_" "-")
keyword))
(defmethod xform :directiveLocation
[prod]
(-> prod
second
second
second
directive-name->keyword))
(defmethod xform :directiveList
[prod]
(mapv xform (rest prod)))
(defmethod xform :directive
[prod]
(let [{:keys [anyName directiveArgList]} (tag prod)]
(-> {:directive-type (xform anyName)}
(common/copy-meta anyName)
(cond->
directiveArgList (assoc :directive-args (xform directiveArgList))))))
(defmethod xform :directiveArgList
[prod]
(checked-map "directive argument"
(->> prod
rest
(map xform))))
(defmethod xform :directiveArg
[prod]
(let [[_ k v] prod]
[(xform k) (xform v)]))
(defmethod xform :fieldDefs
[prod]
(checked-map "field" (map xform (rest prod))))
(defmethod xform :fieldDef
[prod]
(let [{:keys [anyName typeSpec argList description directiveList]} (tag prod)]
[(xform anyName)
(-> {:type (xform typeSpec)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList)
(cond->
argList (assoc :args (xform argList))))]))
(defmethod xform :argList
[prod]
(checked-map "field argument" (map xform (rest prod))))
(defmethod xform :argument
[prod]
(let [{:keys [anyName typeSpec defaultValue description directiveList]} (tag prod)]
[(xform anyName)
(-> {:type (xform typeSpec)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList)
(cond-> defaultValue (assoc :default-value (xform-second defaultValue))))]))
(defmethod xform :value
[prod]
(xform-second prod))
(defmethod xform :enumValue
[prod]
(xform-second prod))
(defmethod xform :booleanvalue
[prod]
(Boolean/valueOf ^String (second prod)))
(defmethod xform :intvalue
[prod]
(Integer/parseInt ^String (second prod)))
(defmethod xform :floatvalue
[prod]
(Float/parseFloat ^String (second prod)))
(defmethod xform :implementationDef
[prod]
(->> prod
rest
(filter #(-> % first (= :name)))
(mapv xform)))
(defmethod xform :typeSpec
[prod]
(let [[_ type required] prod
base-type (-> type xform)]
(if (some? required)
(list 'non-null base-type)
base-type)))
(defmethod xform :listType
[prod]
(list 'list (xform-second prod)))
(defmethod xform :interfaceDef
[prod]
(let [{:keys [anyName fieldDefs description directiveList]
:or {fieldDefs (list :fieldDefs)}} (tag prod)]
[[:interfaces (xform anyName)]
(-> {:fields (xform fieldDefs)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList))]))
(defmethod xform :unionDef
[prod]
(let [{:keys [description anyName unionTypes directiveList]} (tag prod)]
[[:unions (xform anyName)]
(-> {:members (xform unionTypes)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList))]))
(defmethod xform :unionTypes
[prod]
(->> prod
rest
(filter #(-> % first (= :anyName)))
(mapv xform)))
(defmethod xform :enumDef
[prod]
(let [{:keys [description anyName enumValueDefs directiveList]} (tag prod)]
[[:enums (xform anyName)]
(-> {:values (xform enumValueDefs)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList))]))
(defmethod xform :enumValueDefs
[prod]
(mapv xform (rest prod)))
(defmethod xform :enumValueDef
[prod]
(let [{:keys [description nameTokens directiveList]} (tag prod)]
(-> {:enum-value (xform nameTokens)}
(common/copy-meta nameTokens)
(apply-description description)
(apply-directives directiveList))))
(defmethod xform :inputTypeDef
[prod]
(let [{:keys [anyName fieldDefs description directiveList]
:or {fieldDefs (list :fieldDefs)}} (tag prod)]
[[:input-objects (xform anyName)]
(-> {:fields (xform fieldDefs)}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList))]))
(defmethod xform :scalarDef
[prod]
(let [{:keys [anyName description directiveList]} (tag prod)]
[[:scalars (xform anyName)]
(-> {}
(common/copy-meta anyName)
(apply-description description)
(apply-directives directiveList))]))
(defmethod xform :objectValue
[prod]
(-> (mapv xform (rest prod))
(as-> % (checked-map "object key" %))
(common/copy-meta prod)))
(defmethod xform :objectField
[prod]
(let [[_ name value] prod]
[(xform name)
(xform value)]))
(defmethod xform :stringvalue
[prod]
(-> prod second common/stringvalue->String))
(defmethod xform :blockstringvalue
[prod]
(-> prod second common/blockstringvalue->String))
(defmethod xform :arrayValue
[prod]
(common/copy-meta (mapv xform (rest prod)) prod))
(defn ^:private xform-schema
"Given an ANTLR parse tree, returns a Lacinia schema."
[antlr-tree resolvers scalars streamers documentation]
(let [schema (->> antlr-tree
rest
(map xform)
(sort-by (fn [x] (if (is-extension? x) 1 0)))
(reduce (fn [schema [path value]]
(let [path' (butlast path)
k (last path)]
;; Generally, the path is two values (a category such
;; as :objects, and a key within), but there's also
;; [:root] (for the schema production).
(if-not (seq path')
(assoc schema k value)
(update-in schema path'
assoc-check
(-> path' last name) k value))))
{}))]
(-> schema
(attach-field-fns :resolve resolvers)
(attach-field-fns :stream streamers)
;; TODO: This should inject stuff into the scalar, not replace it, right?
(attach-scalars scalars)
(inject-descriptions documentation)
patch-schema-directives)))
(defn parse-schema
"Given a GraphQL schema string, parses it and returns a Lacinia EDN
schema. Defers validation of the schema to the downstream schema
validator.
Directives may be declared and used, but validation of directives is
also deferred downstream.
`attach` should be a map consisting of the following keys:
`:resolvers` is expected to be a map of:
{:type-name {:field-name resolver-fn}}
`:scalars` is expected to be a map of:
{:scalar-name {:parse parse-spec
:serialize serialize-spec}}
A scalar map may also include a :description.
The provided scalar maps are merged into the scalars parsed from the document.
`:streamers` is expected to be a map of:
{:type-name {:subscription-field-name stream-fn}}
`:documentation` is expected to be a map of:
{:type-name doc-str
:type-name/field-name doc-str
:type-name/field-name.arg-name doc-str}"
[schema-string attach]
(when-let [ed (s/explain-data ::parse-schema-args [schema-string attach])]
(throw (ex-info (str "Arguments to parse-schema do not conform to spec:\n" (with-out-str (s/explain-out ed)))
ed)))
(let [{:keys [resolvers scalars streamers documentation]} attach]
(xform-schema (try
(common/antlr-parse grammar schema-string)
(catch ParseError e
(let [failures (common/parse-failures e)]
(throw (ex-info "Failed to parse GraphQL schema."
{:errors failures})))))
resolvers
scalars
streamers
documentation)))
(s/def ::field-fn (s/map-of simple-keyword? (s/or :function ::schema/function-or-var
:keyword simple-keyword?)))
(s/def ::fn-map (s/map-of simple-keyword? ::field-fn))
(s/def ::parse ::schema/parse-or-serialize-fn)
(s/def ::serialize ::schema/parse-or-serialize-fn)
(s/def ::scalar-def (s/keys :req-un [::parse ::serialize]
:opt-un [::description]))
(s/def ::description string?)
(s/def ::documentation (s/map-of keyword? string?))
(s/def ::scalars (s/map-of simple-keyword? ::scalar-def))
(s/def ::resolvers ::fn-map)
(s/def ::streamers ::fn-map)
(s/def ::parse-schema-args (s/cat :schema-string string?
:attachments (s/keys :opt-un [::resolvers
::streamers
::scalars
::documentation])))