-
Notifications
You must be signed in to change notification settings - Fork 10
/
spec.cljc
35 lines (28 loc) · 989 Bytes
/
spec.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
(ns qlkit.spec
(:require [clojure.spec.alpha :as s]))
(def ^:dynamic *defining-component?*)
(defn mutation-query? [k]
(= \! (last (name k))))
(defn contains-mutation? [query-term]
(->> query-term
(filter vector?)
(map first)
(some mutation-query?)))
(s/def ::component-definition-rules
(fn [query-term]
(if *defining-component?*
(not (contains-mutation? query-term))
true)))
(s/def ::query-term
(s/spec (s/and vector?
::component-definition-rules
(s/cat
:tag keyword?
:attrs (s/? map?)
:children (s/* ::query-term)))))
(s/def ::query
(s/spec (s/and vector? (s/cat :query (s/* ::query-term)))))
(defn query-spec [query & [defining-component?]]
(binding [*defining-component?* defining-component?]
(when-not (s/valid? ::query query)
(throw (ex-info (str "Invalid query: \n" query "\n" (with-out-str (s/explain ::query query))) {})))))