/
indexes.cljc
112 lines (96 loc) · 3.22 KB
/
indexes.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
(ns com.wsscode.pathom.connect.indexes
(:require
[clojure.spec.alpha :as s]
[com.fulcrologic.guardrails.core :refer [>def >defn >fdef => | <- ?]]
[com.wsscode.pathom.core :as p]))
(>def :com.wsscode.pathom.connect/sym symbol?)
(>def :com.wsscode.pathom.connect/attribute ::p/attribute)
(>def :com.wsscode.pathom.connect/attributes-set (s/coll-of ::p/attribute :kind set?))
(>def :com.wsscode.pathom.connect/io-map (s/map-of :com.wsscode.pathom.connect/attribute :com.wsscode.pathom.connect/io-map))
(declare normalize-io)
(defn resolver-data
"Get resolver map information in env from the resolver sym."
[env-or-indexes sym]
(let [idx (cond-> env-or-indexes
(contains? env-or-indexes :com.wsscode.pathom.connect/indexes)
:com.wsscode.pathom.connect/indexes)]
(get-in idx [:com.wsscode.pathom.connect/index-resolvers sym])))
(defn resolver-provides
[{:com.wsscode.pathom.connect/keys [provides output]}]
(or provides
(if output (normalize-io output))))
; region io map
(defn merge-io-attrs [a b]
(cond
(and (map? a) (map? b))
(merge-with merge-io-attrs a b)
(map? a) a
(map? b) b
:else b))
(>defn normalize-io
"Convert pathom output format into io/provides format."
[output]
[:com.wsscode.pathom.connect/output
=> :com.wsscode.pathom.connect/io-map]
(if (map? output) ; union
(let [unions (into {} (map (fn [[k v]]
[k (normalize-io v)]))
output)
merged (reduce merge-io-attrs (vals unions))]
(assoc merged :com.wsscode.pathom.connect/unions unions))
(into {} (map (fn [x] (if (map? x)
(let [[k v] (first x)]
[k (normalize-io v)])
[x {}])))
output)))
(defn merge-io
"Merge ::p/shape-descriptor maps."
([] {})
([a] a)
([a b]
(merge-with merge-io-attrs a b)))
(defn io->query
"Converts IO format to query format."
[io]
(into []
(map (fn [[k v]]
(if (seq v)
{k (io->query v)}
k)))
io))
(defn merge-oir
"Merge ::index-oir maps."
[a b]
(merge-with #(merge-with into % %2) a b))
(>defn sub-select-io
"Given io-map, filters the parts of it that are also contained in mask."
[io-map mask]
[:com.wsscode.pathom.connect/io-map :com.wsscode.pathom.connect/io-map
=> :com.wsscode.pathom.connect/io-map]
(reduce-kv
(fn [m k v]
(if (contains? io-map k)
(assoc m k (if (seq v) (sub-select-io (get io-map k) v) v))
m))
{}
mask))
(>defn sub-select-ast
"Given an ast and a io-map mask, returns the parts of AST that match the mask."
[{:keys [children] :as ast} mask]
[:edn-query-language.ast/node :com.wsscode.pathom.connect/io-map
=> :edn-query-language.ast/node]
(if (seq children)
(reduce
(fn [ast {:keys [key] :as node}]
(if-let [sub (get mask key)]
(update ast :children conj
(if (:children node)
(if (seq sub)
(sub-select-ast node sub)
(-> node (assoc :type :prop) (dissoc :children)))
node))
ast))
(assoc ast :children [])
children)
ast))
; endregion