-
Notifications
You must be signed in to change notification settings - Fork 2
/
route.clj
49 lines (41 loc) · 1.53 KB
/
route.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
46
47
48
49
(ns phrag.route
"Routes + handlders for reitit and bidi."
(:require [clojure.walk :as w]
[integrant.core :as ig]
[ring.util.response :as ring-res]
[phrag.core :as core]
[phrag.context :as ctx]))
;;; Reitit
(defn- rtt-gql-handler [config]
(let [schema (core/schema config)]
(fn [req]
(let [params (:body-params req)
query (:query params)
vars (:variables params)]
{:status 200
:body (core/exec config schema query vars req)}))))
(defn reitit
"Returns a route setup for reitit at specified path or `/graphql`.
Format: `[\"path\" {:post handler}]`"
[options]
(let [config (ctx/options->config options)]
[(:graphql-path config "/graphql") {:post {:handler (rtt-gql-handler config)}
:middleware (:middleware config)}]))
(defmethod ig/init-key ::reitit [_ options]
(reitit options))
;;; Bidi
(defn- bd-gql-handler [config]
(let [schema (core/schema config)]
(fn [req]
(let [params (:params req)
query (get params "query")
vars (w/keywordize-keys (get params "variables"))]
(ring-res/response (core/exec config schema query vars))))))
(defn bidi
"Returns a route setup for Bidi at specified path or `/graphql`.
Format: `[\"/\" {\"path\" {:post handler}}]`"
[options]
(let [config (ctx/options->config options)]
["/" {(:graphql-path config "graphql") {:post (bd-gql-handler config)}}]))
(defmethod ig/init-key ::bidi [_ options]
(bidi options))