-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
resolvers.cljc
338 lines (286 loc) · 12.1 KB
/
resolvers.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
(ns com.wsscode.pathom3.connect.built-in.resolvers
(:require
[clojure.spec.alpha :as s]
[com.fulcrologic.guardrails.core :refer [<- => >def >defn >fdef ? |]]
[com.wsscode.pathom3.attribute :as p.attr]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.format.eql :as pf.eql])
#?(:cljs
(:require-macros
[com.wsscode.pathom3.connect.built-in.resolvers])))
(>def ::entity-table (s/map-of any? map?))
(defn attr-munge [attr]
(munge (subs (str attr) 1)))
(defn combine-names [na nb]
(cond
(not nb)
na
(not na)
nb
(= na nb)
na
:else
(str (str na "->" nb))))
(defn attr-ns [kw]
(or (namespace kw) "-unqualified"))
(defn attr-alias-resolver-name
([from to]
(symbol
(combine-names (attr-ns from) (attr-ns to))
(combine-names (name from) (name to))))
([from to suffix]
(symbol
(combine-names (attr-ns from) (attr-ns to))
(str (combine-names (name from) (name to)) "--" suffix))))
(defn attr->sym
([kw]
(symbol (attr-ns kw) (name kw)))
([kw suffix]
(symbol (attr-ns kw) (str (name kw) "--" suffix))))
(defn alias-resolver
"Create a resolver that will convert attribute `from` to a attribute `to` with
the same value. This only creates the alias in one direction."
[from to]
(let [resolver-name (attr-alias-resolver-name from to "alias")]
(pco/resolver resolver-name
{::pco/input [from]
::pco/output [to]
::pco/cache? false}
(fn [_ input] {to (get input from)}))))
(defn equivalence-resolver
"Make two attributes equivalent. It's like alias-resolver, but returns a vector containing the alias in both directions."
[attribute-a attribute-b]
[(alias-resolver attribute-a attribute-b)
(alias-resolver attribute-b attribute-a)])
(defn constantly-resolver
"Create a simple resolver that always return `value` for `attribute`."
([attribute value]
(let [resolver-name (attr->sym attribute "const")]
(pco/resolver resolver-name
{::pco/output (if (coll? value)
(pf.eql/data->query {attribute value})
[attribute])
::pco/cache? false}
(fn [_ _] {attribute value})))))
(defn constantly-fn-resolver
"Create a simple resolver that always calls value-fn and return its value. Note that
cache is disabled by default in this resolver."
([attribute value-fn]
(let [resolver-name (attr->sym attribute "const-fn")]
(pco/resolver resolver-name
{::pco/output [attribute]
::pco/cache? false}
(fn [env _] {attribute (value-fn env)})))))
(defn single-attr-resolver
"Apply fn `f` to input `source` and spits the result with the name `target`.
`f` receives a single argument, which is the attribute value from `source`."
[source target f]
(let [resolver-name (attr-alias-resolver-name source target "attr-transform")]
(pco/resolver resolver-name
{::pco/input [source]
::pco/output [target]}
(fn [_ input]
{target (f (get input source))}))))
(defn single-attr-with-env-resolver
"Similar single-attr-resolver, but `f` receives two arguments, `env` and the input."
[source target f]
(let [resolver-name (attr-alias-resolver-name source target "attr-transform")]
(pco/resolver resolver-name
{::pco/input [source]
::pco/output [target]}
(fn [env input]
{target (f env (get input source))}))))
(defn table-output
"For a given static table, compute the accumulated output query of the entity values."
[table]
(let [[{:keys [output]}] (pf.eql/data->query {:output (vec (vals table))})]
output))
(>defn static-table-resolver
"Exposes data for entities, indexes by attr-key. This is a simple way to extend/provide
data for entities using simple Clojure maps. Example:
(def registry
[(pbir/static-table-resolver :song/id
{1 {:song/name \"Marchinha Psicotica de Dr. Soup\"}
2 {:song/name \"There's Enough\"}})
; you can provide a name for the resolver, if so, prefer fully qualified symbols
(pbir/static-table-resolver `song-analysis :song/id
{1 {:song/duration 280 :song/tempo 98}
2 {:song/duration 150 :song/tempo 130}})])
(let [sm (psm/smart-map (pci/register registry)
{:song/id 1})]
(select-keys sm [:song/id :song/name :song/duration]))
; => #:song{:id 1, :name \"Marchinha Psicotica de Dr. Soup\", :duration 280}
In this example, we create two different tables that provides data about songs, the
entities are related by the keys on the table, the `attr-key` says what's the attribute
name to be used to related the data, in this case we use `:song/id` on both, so they
get connected by it.
"
([attribute table]
[::p.attr/attribute ::entity-table
=> ::pco/resolver]
(let [resolver-name (attr->sym attribute "static-table")]
(static-table-resolver resolver-name attribute table)))
([resolver-name attribute table]
[::pco/op-name ::p.attr/attribute ::entity-table
=> ::pco/resolver]
(let [output (table-output table)]
(pco/resolver resolver-name
{::pco/input [attribute]
::pco/output output}
(fn [_ input]
(let [id (get input attribute)]
(get table id)))))))
(>defn static-attribute-map-resolver
"This is like the static-table-resolver, but provides a single attribute on each
map entry.
(def registry
[(pbir/static-attribute-map-resolver :song/id :song/name
{1 \"Marchinha Psicotica de Dr. Soup\"
2 \"There's Enough\"})
(pbir/static-table-resolver `song-analysis :song/id
{1 {:song/duration 280 :song/tempo 98}
2 {:song/duration 150 :song/tempo 130}})])
(let [sm (psm/smart-map (pci/register registry)
{:song/id 1})]
(select-keys sm [:song/id :song/name :song/duration]))
; => #:song{:id 1, :name \"Marchinha Psicotica de Dr. Soup\", :duration 280}
"
[input output mapping]
[::p.attr/attribute ::p.attr/attribute map?
=> ::pco/resolver]
(let [resolver-name (symbol (str (attr-alias-resolver-name input output) "--static-attribute-map"))]
(pco/resolver resolver-name
{::pco/input [input]
::pco/output [output]}
(fn [_ input-map]
(if-let [x (find mapping (get input-map input))]
{output (val x)})))))
(>defn attribute-table-resolver
"Similar to static-table-resolver, but instead of a static map, this will pull the
table from another attribute in the system. Given in this case the values can be dynamic,
this helper requires a pre-defined output, so the attributes on this output get
delegated to the created resolver.
(def registry
[(pbir/static-table-resolver `song-names :song/id
{1 {:song/name \"Marchinha Psicotica de Dr. Soup\"}
2 {:song/name \"There's Enough\"}})
(pbir/constantly-resolver ::song-analysis
{1 {:song/duration 280 :song/tempo 98}
2 {:song/duration 150 :song/tempo 130}})
(pbir/attribute-table-resolver ::song-analysis :song/id
[:song/duration :song/tempo])])
(let [sm (psm/smart-map (pci/register registry)
{:song/id 2})]
(select-keys sm [:song/id :song/name :song/duration]))
; => #:song{:id 2, :name \"There's Enough\", :duration 150}
"
[table-name attr-key output]
[::p.attr/attribute ::p.attr/attribute ::pco/output
=> ::pco/resolver]
(let [resolver-name (symbol (str (attr-munge attr-key) "--table-" (attr-munge table-name)))]
(pco/resolver resolver-name
{::pco/input [attr-key table-name]
::pco/output output}
(fn [_ input]
(let [table (get input table-name)
id (get input attr-key)]
(get table id))))))
(>defn env-table-resolver
"Similar to attribute-table-resolver, but pulls table from env instead of other resolver.
(def registry
[(pbir/static-table-resolver `song-names :song/id
{1 {:song/name \"Marchinha Psicotica de Dr. Soup\"}
2 {:song/name \"There's Enough\"}})
(pbir/env-table-resolver ::song-analysis :song/id
[:song/duration :song/tempo])])
(def table
{::song-analysis
{1 {:song/duration 280 :song/tempo 98}
2 {:song/duration 150 :song/tempo 130}}})
; merge table into env
(let [sm (psm/smart-map (merge (pci/register registry) table)
{:song/id 2})]
(select-keys sm [:song/id :song/name :song/duration]))
; => #:song{:id 2, :name \"There's Enough\", :duration 150}
"
[table-name attr-key output]
[::p.attr/attribute ::p.attr/attribute ::pco/output
=> ::pco/resolver]
(let [resolver-name (symbol (str (attr-munge attr-key) "--env-table-" (attr-munge table-name)))]
(pco/resolver resolver-name
{::pco/input [attr-key]
::pco/output output}
(fn [env input]
(let [table (get env table-name)
id (get input attr-key)]
(get table id))))))
#?(:clj
(defn edn-extract-attr-tables
[data]
`(into []
(keep
(fn [[k# v#]]
(if-let [attr-key# (and (map? v#)
(:com.wsscode.pathom3/entity-table (meta v#)))]
(attribute-table-resolver k# attr-key# (table-output v#)))))
~data)))
#?(:clj
(defmacro edn-file-resolver
"Creates a resolver to provide data loaded from a file.
This is a macro and the file will be read at compilation time, this way it
can work on both Clojure and Clojurescript, without a need for async processing.
It's also possible to provide static tables data (as with attribute-table-resolver)
from the data itself, you can do this by setting the meta data :pathom3/entity-table
meta data in your EDN data. For example:
{:my.system/generic-db
^{:com.wsscode.pathom3/entity-table :my.system/user-id}
{4 {:my.system.user/name \"Anne\"}
2 {:my.system.user/name \"Fred\"}}}
Doing this, the resolvers for the attribute table will be provided automatically.
Full example:
; my-config.edn
{:my.system/port
1234
:my.system/initial-path
\"/tmp/system\"
:my.system/generic-db
^{:com.wsscode.pathom3/entity-table :my.system/user-id}
{4 {:my.system.user/name \"Anne\"}
2 {:my.system.user/name \"Fred\"}}}
; app
(def registry [(edn-file-resolver \"my-config.edn\")])
(let [sm (psm/smart-map (pci/register registry) {:my.system/user-id 4})]
(select-keys sm [:my.system/port :my.system.user/name])
; => {:my.system/port 1234, :my.system.user/name \"Anne\"}
Note that the tables need to be a value of a top level attribute of the config, if
its deeper inside it won't work."
[file-path]
(let [data (read-string (slurp file-path))
resolver-name (symbol "com.wsscode.pathom3.edn-file-resolver" (munge file-path))
output (pf.eql/data->query data)
attr-tables (edn-extract-attr-tables data)]
`[(pco/resolver '~resolver-name
{::pco/output ~output}
(fn ~'[_ _] ~data))
~attr-tables])))
(defn extract-attr-tables
[data]
(into []
(keep
(fn [[k v]]
(if-let [attr-key (and (map? v)
(:com.wsscode.pathom3/entity-table (meta v)))]
(attribute-table-resolver k attr-key (table-output v)))))
data))
(defn global-data-resolver
"Expose data as a resolver, note this data will be available everywhere in the system.
Works the same as edn-file-resolver, but uses the data directly instead of reading
from a file. This also applies for the attribute tables inside the data."
[data]
(let [resolver-name (symbol "com.wsscode.pathom3.global-data-resolver" (str (hash data)))
output (pf.eql/data->query data)
attr-tables (extract-attr-tables data)]
[(pco/resolver resolver-name
{::pco/output output}
(fn global-data-resolver-fn [_ _] data))
attr-tables]))