-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
45 lines (37 loc) · 1.42 KB
/
core.clj
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
(ns promecinia.core
(:require [com.walmartlabs.lacinia.resolve :as lr]
[promesa.core :as p])
(:import [java.util.concurrent CompletableFuture]))
(defn show-stack-trace
"Translate all internal errors to GraphQL errors. Don't do this in production."
[x]
(if (instance? Throwable x)
(let [{:as error :keys [cause]} (Throwable->map x)]
(lr/with-error nil (assoc error :message cause)))
x))
(defn show-stack-traces
"Translate all internal errors to GraphQL errors. Don't do this in production."
[e]
(let [nil-with-error (show-stack-trace e)]
(repeatedly (constantly nil-with-error))))
(def ^:const internal-server-error {:message "Internal Server Error."})
(extend-protocol com.walmartlabs.lacinia.resolve/ResolverResult
CompletableFuture
(on-deliver! [promise callback]
(p/handle promise
(fn [value error]
(let [result (cond-> value
error (lr/with-error internal-server-error))]
(callback result))))
promise))
(defn attach-context [promise context]
(p/then promise #(lr/with-context % context)))
(defn attach-error [promise error]
(p/then promise #(lr/with-error % error)))
(defn attach-warning [promise warning]
(p/then promise #(lr/with-warning % warning)))
(defn attach [promise {:keys [error warning context]}]
(cond-> promise
context (attach-context context)
error (attach-error error)
warning (attach-warning warning)))