Skip to content

Commit 34bca5f

Browse files
authored
graphql playground middleware (#2)
Add a middleware to server another graphql IDE, namely https://github.com/prisma-labs/graphql-playground/
1 parent b2296ed commit 34bca5f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
[metosin/ring-http-response "0.9.0"]
99
[ring/ring-core "1.6.0"]
1010
[threatgrid/ring-graphiql "0.1.1"]
11-
[threatgrid/ring-graphql-voyager "0.1.2"]]
11+
[threatgrid/ring-graphql-voyager "0.1.2"]
12+
[viebel/ring-graphql-playground "0.1.1"]]
1213
:profiles {:test {:dependencies [[ring/ring-mock "0.3.0"]]}})

src/ring_graphql_ui/core.clj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

9595
(defn voyager
9696
"Returns a Ring handler which can be used to serve GraphQL Voyager"
97-
([] (graphiql {}))
97+
([] (voyager {}))
9898
([options]
9999
(serve-ui {:path "/voyager"
100100
:root "graphql-voyager"}
@@ -107,3 +107,21 @@
107107
(wrap-ui handler voyager))
108108
([handler options]
109109
(wrap-ui handler voyager options)))
110+
111+
;;-------- GraphQL Playground
112+
113+
(defn playground
114+
"Returns a Ring handler which can be used to serve GraphQL Playground https://github.com/prisma-labs/graphql-playground#as-html-page"
115+
([] (playground {}))
116+
([options]
117+
(serve-ui {:path "/playground"
118+
:root "graphql-playground"}
119+
options
120+
"GRAPHQL_PLAYGROUND_CONF")))
121+
122+
(defn wrap-playground
123+
"Middleware to serve GraphQL Playground."
124+
([handler]
125+
(wrap-ui handler playground))
126+
([handler options]
127+
(wrap-ui handler playground options)))

test/ring_graphql_ui/core_test.clj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,23 @@
9999
:endpoint "ctia/graphql"}))]
100100
(is (html? (http-get handler "/voyager/index.html")))
101101
(is (nil? (http-get handler "unknown")))))
102+
103+
(deftest playground-test
104+
(let [handler (sut/playground {:path "/playground"
105+
:endpoint "ctia/graphql"})]
106+
(testing "index.html"
107+
(is (redirect? (http-get handler "/playground")))
108+
(is (html? (http-get handler "/playground/index.html"))))
109+
(testing "conf.js"
110+
(let [conf-response (http-get handler "/playground/conf.js")]
111+
(is (javascript? conf-response))
112+
(is (= {:endpoint "ctia/graphql"}
113+
(read-js (:body conf-response)
114+
"GRAPHQL_PLAYGROUND_CONF")))))))
115+
116+
(deftest wrap-playground-test
117+
(let [handler (-> (constantly nil)
118+
(sut/wrap-graphiql {:path "/playground"
119+
:endpoint "ctia/graphql"}))]
120+
(is (html? (http-get handler "/playground/index.html")))
121+
(is (nil? (http-get handler "unknown")))))

0 commit comments

Comments
 (0)