/
sugar.cljc
71 lines (63 loc) · 3 KB
/
sugar.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
(ns com.wsscode.pathom.sugar
"This namespace contains easy ways to setup common configurations for Pathom parsers"
(:require [com.wsscode.pathom.core :as p]
[com.wsscode.pathom.connect :as pc]))
(defn connect-serial-parser
"Create a standard connect parser using the serial parser.
This parser recommended for handling small and simple queries, like
resolvers to process missing configuration options."
[register]
(p/parser
{::p/env {::p/reader [p/map-reader
pc/reader2
pc/open-ident-reader
p/env-placeholder-reader]
::p/placeholder-prefixes #{">"}}
::p/mutate pc/mutate
::p/plugins [(pc/connect-plugin {::pc/register register})
p/error-handler-plugin
p/trace-plugin]}))
(defn connect-async-parser
"Create a standard connect parser using the async parser.
Just like the serial parser, but supports waiting for core.async channels
in responses. The most common usage of this one is in ClojureScript land, where
most of the IO needs to be async."
[register]
(p/async-parser
{::p/env {::p/reader [p/map-reader
pc/async-reader2
pc/open-ident-reader
p/env-placeholder-reader]
::p/placeholder-prefixes #{">"}}
::p/mutate pc/mutate-async
::p/plugins [(pc/connect-plugin {::pc/register register})
p/error-handler-plugin
p/trace-plugin]}))
(defn connect-parallel-parser
"Create a standard connect parser using the parallel parser.
This is recommended if you have a lot of different information sources that
are IO bound. This parser can handle things in parallel, but adds extra overhead
to processing, use it in case your system has good parallelism opportunities."
[register]
(p/parallel-parser
{::p/env {::p/reader [p/map-reader
pc/parallel-reader
pc/open-ident-reader
p/env-placeholder-reader]
::p/placeholder-prefixes #{">"}}
::p/mutate pc/mutate-async
::p/plugins [(pc/connect-plugin {::pc/register register})
p/error-handler-plugin
p/trace-plugin]}))
(defn context-parser
"Transforms the signature of a regular parser to one that takes
some initial context to run the query. This returns a fn with
the following arities:
[context query] => runs query using context as initial data.
[env context query] => same as before but accepts initial environment."
[parser]
(fn context-parser-internal
([context query]
(parser {::p/entity (atom context)} query))
([env context query]
(parser (assoc env ::p/entity (atom context)) query))))