-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
operation.cljc
586 lines (478 loc) · 19.5 KB
/
operation.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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
(ns com.wsscode.pathom3.connect.operation
(:require
[clojure.set :as set]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[com.fulcrologic.guardrails.core :refer [<- => >def >defn >fdef |]]
[com.wsscode.misc.coll :as coll]
#?(:clj [com.wsscode.misc.macros :as macros])
[com.wsscode.misc.refs :as refs]
[com.wsscode.pathom3.attribute :as p.attr]
[com.wsscode.pathom3.connect.operation.protocols :as pop]
[com.wsscode.pathom3.format.eql :as pf.eql]
[com.wsscode.pathom3.format.shape-descriptor :as pfsd]
[edn-query-language.core :as eql])
#?(:cljs
(:require-macros
[com.wsscode.pathom3.connect.operation])))
; region type predicates
(defn operation? [x] (satisfies? pop/IOperation x))
(defn resolver? [x] (satisfies? pop/IResolver x))
(defn mutation? [x] (satisfies? pop/IMutation x))
; endregion
; region specs
(>def ::op-name "Name of the operation" symbol?)
(>def ::input vector?)
(>def ::output vector?)
(>def ::params vector?)
(>def ::docstring string?)
(>def ::cache? boolean?)
(>def ::cache-store keyword?)
(>def ::cache-key fn?)
(>def ::batch? boolean?)
(>def ::batch-chunk-size pos-int?)
(>def ::priority int?)
(>def ::resolve fn?)
(>def ::mutate fn?)
(>def ::operation-type #{::operation-type-resolver ::operation-type-mutation})
(>def ::operation-config map?)
(>def ::operation operation?)
(>def ::resolver resolver?)
(>def ::mutation mutation?)
(>def ::provides ::pfsd/shape-descriptor)
(>def ::requires ::pfsd/shape-descriptor)
(>def ::optionals ::pfsd/shape-descriptor)
(>def ::dynamic-name ::op-name)
(>def ::dynamic-resolver? boolean?)
(>def ::transform fn?)
; endregion
; region records
(defrecord Resolver [config resolve]
pop/IOperation
(-operation-config [_] config)
(-operation-type [_] ::operation-type-resolver)
pop/IResolver
(-resolve [_ env input] (resolve env input))
#?@(:bb
[]
:clj
[clojure.lang.IFn
(invoke [_this] (resolve {} {}))
(invoke [_this input] (resolve {} input))
(invoke [_this env input] (resolve env input))]
:cljs
[IFn
(-invoke [_this] (resolve {} {}))
(-invoke [_this input] (resolve {} input))
(-invoke [_this env input] (resolve env input))]))
(defrecord Mutation [config mutate]
pop/IOperation
(-operation-config [_] config)
(-operation-type [_] ::operation-type-mutation)
pop/IMutation
(-mutate [_ env input] (mutate env input))
#?@(:bb
[]
:clj
[clojure.lang.IFn
(invoke [_this] (mutate {} {}))
(invoke [_this input] (mutate {} input))
(invoke [_this env input] (mutate env input))]
:cljs
[IFn
(-invoke [_this] (mutate {} {}))
(-invoke [_this input] (mutate {} input))
(-invoke [_this env input] (mutate env input))]))
; endregion
; region constructors and helpers
(>defn ?
"Make an attribute optional"
[attr]
[::p.attr/attribute => any?]
(eql/update-property-param attr assoc ::optional? true))
(>defn operation-config [operation]
[::operation => ::operation-config]
(pop/-operation-config operation))
(>defn operation-type [operation]
[::operation => ::operation-type]
(pop/-operation-type operation))
(defn describe-input*
[ast path outs* opt-parent?]
(doseq [{:keys [key params] :as node} (:children ast)]
(let [entry (cond-> {}
(seq params)
(with-meta {::pfsd/params params}))
opt? (or opt-parent? (::optional? params))]
(if opt?
(vswap! outs* assoc-in (concat [::optionals] path [key]) entry)
(vswap! outs* assoc-in (concat [::requires] path [key]) entry))
(describe-input* node (conj path key) outs* opt?))))
(defn describe-input [input]
(let [input-ast (eql/query->ast input)
outs* (volatile! {::requires {}})]
(describe-input* input-ast [] outs* false)
@outs*))
(defn- eql->root-attrs [eql]
(->> eql eql/query->ast :children (into #{} (map :key))))
(defn input-destructure-missing [{::keys [input inferred-input]}]
(if (and input
inferred-input)
(let [missing (set/difference
(eql->root-attrs inferred-input)
(eql->root-attrs input))]
(if (seq missing) missing))))
(>defn resolver
"Helper to create a resolver. A resolver have at least a name, the output definition
and the resolve function.
You can create a resolver using a map:
(resolver
{::op-name 'foo
::output [:foo]
::resolve (fn [env input] ...)})
Or with the helper syntax:
(resolver 'foo {::output [:foo]} (fn [env input] ...))
Returns an instance of the Resolver type.
"
([op-name config]
[::op-name (s/keys :opt [::input ::output ::params]) => ::resolver]
(resolver (-> config
(coll/merge-defaults {::op-name op-name}))))
([op-name config resolve]
[::op-name (s/keys :opt [::input ::output ::params]) ::resolve => ::resolver]
(resolver (-> config
(coll/merge-defaults {::op-name op-name})
(assoc ::resolve resolve))))
([{::keys [transform op-name inferred-input input] :as config}]
[(s/or :map (s/keys :req [::op-name] :opt [::input ::output ::resolve ::transform])
:resolver ::resolver)
=> ::resolver]
(let [config (if (resolver? config)
config
(cond-> config transform transform))]
(when-not (s/valid? (s/keys) config)
(s/explain (s/keys) config)
(throw (ex-info (str "Invalid config on resolver " name)
{:explain-data (s/explain-data (s/keys) config)})))
(if-not (::disable-validate-input-destructuring? config)
(if-let [missing (input-destructure-missing config)]
(throw (ex-info
(str "Input of resolver " op-name " destructuring requires attributes \"" (str/join "," missing) "\" that are not present at the input definition.")
{::input input
::inferred-input inferred-input}))))
(if (resolver? config)
config
(let [{::keys [resolve output]} config
defaults (if output
{::input []
::provides (pfsd/query->shape-descriptor output)}
{})
{::keys [input] :as config'}
(-> (merge defaults config)
(dissoc ::resolve ::transform))
config' (cond-> config'
input
(merge (describe-input input)))]
(->Resolver config' (or resolve (fn [_ _]))))))))
(>defn mutation
"Helper to create a mutation. A mutation must have a name and the mutate function.
You can create a mutation using a map:
(mutation
{::op-name 'foo
::output [:foo]
::mutate (fn [env params] ...)})
Or with the helper syntax:
(mutation 'foo {} (fn [env params] ...))
Returns an instance of the Mutation type.
"
([op-name config]
[::op-name (s/keys :opt [::output ::params]) => ::mutation]
(mutation (-> config
(coll/merge-defaults {::op-name op-name}))))
([op-name config mutate]
[::op-name (s/keys :opt [::output ::params]) ::mutate => ::mutation]
(mutation (-> config
(coll/merge-defaults {::op-name op-name})
(assoc ::mutate mutate))))
([{::keys [transform] :as config}]
[(s/or :map (s/keys :req [::op-name] :opt [::output ::mutate ::transform])
:mutation ::mutation)
=> ::mutation]
(when-not (s/valid? (s/keys) config)
(s/explain (s/keys) config)
(throw (ex-info (str "Invalid config on mutation " name)
{:explain-data (s/explain-data (s/keys) config)})))
(if (mutation? config)
config
(let [{::keys [mutate output] :as config} (cond-> config transform transform)
defaults (if output
{::provides (pfsd/query->shape-descriptor output)}
{})
config' (-> (merge defaults config)
(dissoc ::mutate ::transform))]
(->Mutation config' (or mutate (fn [_ _])))))))
(>defn params
"Pull parameters from environment. Always returns a map."
[env]
[map? => map?]
(or (get-in env [:com.wsscode.pathom3.connect.planner/node
:com.wsscode.pathom3.connect.planner/params])
{}))
(>defn with-node-params
"Set current node params to params."
([params]
[map? => map?]
{:com.wsscode.pathom3.connect.planner/node
{:com.wsscode.pathom3.connect.planner/params
params}})
([env params]
[map? map? => map?]
(assoc-in env [:com.wsscode.pathom3.connect.planner/node
:com.wsscode.pathom3.connect.planner/params]
params)))
; endregion
; region macros
#?(:clj
(do
(s/def ::simple-keys-binding
(s/tuple #{:keys} (s/coll-of ident? :kind vector?)))
(s/def ::qualified-keys-binding
(s/tuple
(s/and qualified-keyword? #(= (name %) "keys"))
(s/coll-of simple-symbol? :kind vector?)))
(s/def ::as-binding
(s/tuple #{:as} simple-symbol?))
(s/def ::map-destructure
(s/every
(s/or :simple-keys-binding ::simple-keys-binding
:qualified-keys-bindings ::qualified-keys-binding
:named-extract (s/tuple ::operation-argument keyword?)
:as ::as-binding)
:kind map?))
(s/def ::operation-argument
(s/or :sym symbol?
:map ::map-destructure))
(s/def ::operation-args
(s/coll-of ::operation-argument :kind vector? :min-count 0 :max-count 2))
(s/def ::defresolver-args
(s/and
(s/cat :name simple-symbol?
:docstring (s/? string?)
:arglist ::operation-args
:options (s/? map?)
:body (s/+ any?))
(fn must-have-output-visible-map-or-options [{:keys [body options]}]
(or (map? (last body)) options))))
(s/def ::defmutation-args
(s/and
(s/cat :name simple-symbol?
:docstring (s/? string?)
:arglist ::operation-args
:options (s/? map?)
:body (s/+ any?)))))
:cljs
(s/def ::defresolver-args any?))
(defn as-entry? [x] (refs/kw-identical? :as (first x)))
(defn extract-destructure-map-keys-as-keywords [m]
(into []
(comp
(remove as-entry?)
(mapcat
(fn [[k val]]
(if (and (keyword? k)
(= "keys" (name k)))
(map #(keyword (or (namespace %)
(namespace k)) (name %)) val)
[val]))))
m))
(defn params->resolver-options [{:keys [arglist options body docstring]}]
(let [[input-type input-arg] (last arglist)
last-expr (last body)
inferred-input (if (refs/kw-identical? :map input-type)
(extract-destructure-map-keys-as-keywords input-arg))]
(cond-> options
(and (map? last-expr) (not (::output options)))
(assoc ::output (pf.eql/data->query last-expr))
inferred-input
(assoc ::inferred-input inferred-input)
(and inferred-input
(not (::input options)))
(assoc ::input inferred-input)
docstring
(assoc ::docstring docstring))))
(defn params->mutation-options [{:keys [arglist options body docstring]}]
(let [[input-type params-arg] (last arglist)
last-expr (last body)]
(cond-> options
(and (map? last-expr) (not (::output options)))
(assoc ::output (pf.eql/data->query last-expr))
(and (refs/kw-identical? :map input-type)
(not (::params options)))
(assoc ::params (extract-destructure-map-keys-as-keywords params-arg))
docstring
(assoc ::docstring docstring))))
(defn normalize-arglist
"Ensures arglist contains two elements."
[arglist]
(loop [arglist arglist]
(if (< (count arglist) 2)
(recur (into '[[:sym _]] arglist))
arglist)))
#?(:clj
(defmacro defresolver
"Defines a new Pathom resolver.
Resolvers are the central abstraction around Pathom, a resolver is a function
that contains some annotated information and follow a few rules:
1. The resolver input must be a map, so the input information is labelled.
2. A resolver must return a map, so the output information is labelled.
3. A resolver also receives a separated map containing the environment information.
Here are some examples of how you can use the defresolver syntax to define resolvers:
The verbose example:
(pco/defresolver song-by-id [env {:acme.song/keys [id]}]
{::pco/input [:acme.song/id]
::pco/output [:acme.song/title :acme.song/duration :acme.song/tone]
::pco/params []
::pco/transform identity}
(fetch-song env id))
The previous example demonstrates the usage of the most common options in defresolver.
But we don't need to write all of that, for example, instead of manually saying
the ::pco/input, we can let the defresolver infer it from the param destructuring, so
the following code works the same (::pco/params and ::pco/transform also removed, since
they were no-ops in this example):
(pco/defresolver song-by-id [env {:acme.song/keys [id]}]
{::pco/output [:acme.song/title :acme.song/duration :acme.song/tone]}
(fetch-song env id))
This makes for a cleaner write, now lets use this format and write a new example
resolver:
(pco/defresolver full-name [env {:acme.user/keys [first-name last-name]}]
{::pco/output [:acme.user/full-name]}
{:acme.user/full-name (str first-name \" \" last-name)})
The first thing we see is that we don't use env, so we can omit it.
(pco/defresolver full-name [{:acme.user/keys [first-name last-name]}]
{::pco/output [:acme.user/full-name]}
{:acme.user/full-name (str first-name \" \" last-name)})
Also, when the last expression of the defresolver is a map, it will infer the output
shape from it:
(pco/defresolver full-name [{:acme.user/keys [first-name last-name]}]
{:acme.user/full-name (str first-name \" \" last-name)})
You can always override the implicit input and output by setting on the configuration
map.
Standard options:
::pco/output - description of resolver output, in EQL format
::pco/input - description of resolver input, in EQL format
::pco/params - description of resolver parameters, in EQL format
::pco/transform - a function to transform the resolver configuration before instantiating the resolver
Caching options:
::pco/cache? - true by default, set to false to disable caching for the resolver
::pco/cache-store - a keyword pointing a cache store in the environment
::pco/cache-key - a function of [env input-data] to use as a key when caching this resolver call
Batching options:
::pco/batch? - set true to indicate this resolver does batch processing
::pco/batch-chunk-size - the max size of each batch chunk, when set Pathom will
split the batch groups in groups of at max batch-chunk-size
items
Note that any other option that you send to the resolver config will be stored in the
index and can be read from it at any time.
The returned value is of the type Resolver, you can test your resolver by calling
directly:
(full-name {:acme.user/first-name \"Ada\"
:acme.user/last-name \"Lovelace\"})
=> \"Ada Lovelace\"
Note that similar to the way we define the resolver, we can also omit `env` (and even
the input) when calling, the resolvers fns always support arity 0, 1 and 2.
"
{:arglists '([name docstring? arglist options? & body])}
[& args]
(let [{:keys [name docstring arglist body] :as params}
(-> (s/conform ::defresolver-args args)
(update :arglist normalize-arglist))
arglist' (s/unform ::operation-args arglist)
fqsym (macros/full-symbol name (str *ns*))
defdoc (cond-> [] docstring (conj docstring))]
`(def ~name
~@defdoc
(resolver '~fqsym ~(params->resolver-options (assoc params ::op-name fqsym))
(fn ~name ~arglist'
~@body))))))
#?(:clj
(s/fdef defresolver
:args ::defresolver-args
:ret any?))
#?(:clj
(defmacro defmutation
"Defines a new Pathom mutation. The syntax of this macro is similar to defresolver,
But where `defresolver` takes input, `defmutation` uses as ::params."
{:arglists '([name docstring? arglist options? & body])}
[& args]
(let [{:keys [name docstring arglist body] :as params}
(-> (s/conform ::defmutation-args args)
(update :arglist normalize-arglist))
arglist' (s/unform ::operation-args arglist)
fqsym (macros/full-symbol name (str *ns*))
defdoc (cond-> [] docstring (conj docstring))]
`(def ~name
~@defdoc
(mutation '~fqsym ~(params->mutation-options params)
(fn ~name ~arglist'
~@body))))))
#?(:clj
(s/fdef defmutation
:args ::defmutation-args
:ret any?))
(defn update-config
"Returns a new resolver with the modified config. You can use this to change anything
in the resolver configuration map. The only thing you can't change from here is the
resolver or mutation functions. You can use the wrap-resolve and wrap-mutation
helpers to do that."
([operation f]
(update operation :config f))
([operation f a1]
(update operation :config f a1))
([operation f a1 a2]
(update operation :config f a1 a2))
([operation f a1 a2 a3]
(update operation :config f a1 a2 a3))
([operation f a1 a2 a3 a4]
(update operation :config f a1 a2 a3 a4))
([operation f a1 a2 a3 a4 a5]
(update operation :config f a1 a2 a3 a4 a5))
([operation f a1 a2 a3 a4 a5 a6]
(update operation :config f a1 a2 a3 a4 a5 a6))
([operation f a1 a2 a3 a4 a5 a6 a7]
(update operation :config f a1 a2 a3 a4 a5 a6 a7))
([operation f a1 a2 a3 a4 a5 a6 a7 a8]
(update operation :config f a1 a2 a3 a4 a5 a6 a7 a8))
([operation f a1 a2 a3 a4 a5 a6 a7 a8 a9]
(update operation :config f a1 a2 a3 a4 a5 a6 a7 a8 a9))
([operation f a1 a2 a3 a4 a5 a6 a7 a8 a9 & args]
(apply update operation :config f a1 a2 a3 a4 a5 a6 a7 a8 a9 args)))
(defn wrap-resolve
"Return a new resolver with the resolve fn modified. You can use the previous fn or
just replace. Here is a noop wrapper example:
(wrap-resolve resolver
(fn [resolve]
(fn [env input]
(resolve env input)))"
[resolver f]
(update resolver :resolve f))
(defn wrap-mutate
"Return a new mutation with the resolve fn modified. You can use the previous fn or
just replace. Here is a noop wrapper example:
(wrap-mutate mutation
(fn [mutate]
(fn [env params]
(mutate env params)))"
[mutation f]
(update mutation :mutate f))
(defn final-value
"Makes a value final. This will add some meta-data to a collection or a map to
flag it as final. Data marked as final will make Pathom skip sub-processing it.
Example:
(pco/defresolver complex-done-list []
{:dont-process (pco/final-value [{:a 1} {:a 2} ...])})
Note that in this case, a query like `[{:dont-process [:a :b]}]` won't even try
to process `:b`, the vector value will be returned as-is."
[x]
(vary-meta x assoc ::final true))
(defn final-value? [x]
(some-> x meta ::final true?))
; endregion