-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathgraphiql.ex
476 lines (379 loc) · 12.4 KB
/
graphiql.ex
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
defmodule Absinthe.Plug.GraphiQL do
@moduledoc """
Provides a GraphiQL interface.
## Examples
The examples here are shown in
Serve the GraphiQL "advanced" interface at `/graphiql`, but only in
development:
if Mix.env == :dev do
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [schema: MyAppWeb.Schema]
end
Use the "simple" interface (original GraphiQL) instead:
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
interface: :simple
]
Finally there is also support for GraphiQL Playground
https://github.com/graphcool/graphql-playground
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
interface: :playground
]
## Interface Selection
The GraphiQL interface can be switched using the `:interface` option.
- `:advanced` (default) will serve the [GraphiQL Workspace](https://github.com/OlegIlyenko/graphiql-workspace) interface from Oleg Ilyenko.
- `:simple` will serve the original [GraphiQL](https://github.com/graphql/graphiql) interface from Facebook.
- `:playground` will serve the [GraphQL Playground](https://github.com/graphcool/graphql-playground) interface from Graphcool.
See `Absinthe.Plug` for the other options.
## Default Headers
You can optionally provide default headers if the advanced interface (GraphiQL Workspace) is selected.
Note that you may have to clean up your existing workspace by clicking the trashcan icon in order to see the newly set default headers.
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
default_headers: {__MODULE__, :graphiql_headers}
]
def graphiql_headers do
%{
"X-CSRF-Token" => Plug.CSRFProtection.get_csrf_token(),
"X-Foo" => "Bar"
}
end
You can also provide a function that takes a conn argument if you need to access connection data
(e.g. if you need to set an Authorization header based on the currently logged-in user).
def graphiql_headers(conn) do
%{
"Authorization" => "Bearer " <> conn.assigns[:token]
}
end
## Default URL
You can also optionally set the default URL to be used for sending the queries to.
This only applies to the advanced interface (GraphiQL Workspace) and the GraphQL Playground.
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
default_url: "https://api.mydomain.com/graphql"
]
This option also accepts a function:
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
default_url: {__MODULE__, :graphiql_default_url}
]
def graphiql_default_url(conn) do
conn.assigns[:graphql_url]
end
## Socket URL
You can also optionally set the default websocket URL to be used for subscriptions.
This only applies to the advanced interface (GraphiQL Workspace) and the GraphQL Playground.
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
socket_url: "wss://api.mydomain.com/socket"
]
This option also accepts a function:
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
socket_url: {__MODULE__, :graphiql_socket_url}
]
def graphiql_socket_url(conn) do
conn.assigns[:graphql_socket_url]
end
"""
require EEx
@graphiql_template_path Path.join(__DIR__, "graphiql")
EEx.function_from_file(
:defp,
:graphiql_html,
Path.join(@graphiql_template_path, "graphiql.html.eex"),
[:query_string, :variables_string, :result_string, :socket_url, :assets]
)
EEx.function_from_file(
:defp,
:graphiql_workspace_html,
Path.join(@graphiql_template_path, "graphiql_workspace.html.eex"),
[:query_string, :variables_string, :default_headers, :default_url, :socket_url, :assets]
)
EEx.function_from_file(
:defp,
:graphiql_playground_html,
Path.join(@graphiql_template_path, "graphiql_playground.html.eex"),
[:default_url, :socket_url, :assets]
)
@behaviour Plug
import Plug.Conn
@type opts :: [
schema: atom,
adapter: atom,
path: binary,
context: map,
json_codec: atom | {atom, Keyword.t()},
interface: :playground | :advanced | :simple,
default_headers: {module, atom},
default_url: binary,
assets: Keyword.t(),
socket: module,
socket_url: binary
]
@doc false
@spec init(opts :: opts) :: map
def init(opts) do
assets = Absinthe.Plug.GraphiQL.Assets.get_assets()
opts
|> Absinthe.Plug.init()
|> Map.put(:interface, Keyword.get(opts, :interface) || :advanced)
|> Map.put(:default_headers, Keyword.get(opts, :default_headers))
|> Map.put(:default_url, Keyword.get(opts, :default_url))
|> Map.put(:assets, assets)
|> Map.put(:socket, Keyword.get(opts, :socket))
|> Map.put(:socket_url, Keyword.get(opts, :socket_url))
|> Map.put(:default_query, Keyword.get(opts, :default_query, ""))
|> set_pipeline
end
@doc false
def call(conn, config) do
case html?(conn) do
true -> do_call(conn, config)
_ -> Absinthe.Plug.call(conn, config)
end
end
defp html?(conn) do
Plug.Conn.get_req_header(conn, "accept")
|> List.first()
|> case do
string when is_binary(string) ->
String.contains?(string, "text/html")
_ ->
false
end
end
defp do_call(conn, %{interface: interface} = config) do
config =
config
|> handle_default_headers(conn)
|> put_config_value(:default_url, conn)
|> handle_socket_url(conn)
with {:ok, conn, request} <- Absinthe.Plug.Request.parse(conn, config),
{:process, request} <- select_mode(request),
{:ok, request} <- Absinthe.Plug.ensure_processable(request, config),
:ok <- Absinthe.Plug.Request.log(request, config.log_level) do
conn_info = %{
conn_private: (conn.private[:absinthe] || %{}) |> Map.put(:http_method, conn.method)
}
{conn, result} = Absinthe.Plug.run_request(request, conn, conn_info, config)
case result do
{:ok, result} ->
# GraphiQL doesn't batch requests, so the first query is the only one
query = hd(request.queries)
{:ok, conn, result, query.variables, query.document || ""}
{:error, {:http_method, _}, _} ->
query = hd(request.queries)
{:http_method_error, query.variables, query.document || ""}
other ->
other
end
end
|> case do
{:ok, conn, result, variables, query} ->
query = query |> js_escape
var_string =
variables
|> config.json_codec.module.encode!(pretty: true)
|> js_escape
result =
result
|> config.json_codec.module.encode!(pretty: true)
|> js_escape
config =
%{
query: query,
var_string: var_string,
result: result
}
|> Map.merge(config)
conn
|> render_interface(interface, config)
{:input_error, msg} ->
conn
|> send_resp(400, msg)
:start_interface ->
conn
|> render_interface(interface, config)
{:http_method_error, variables, query} ->
query = query |> js_escape
var_string =
variables
|> config.json_codec.module.encode!(pretty: true)
|> js_escape
config =
%{
query: query,
var_string: var_string
}
|> Map.merge(config)
conn
|> render_interface(interface, config)
{:error, error, _} when is_binary(error) ->
conn
|> send_resp(500, error)
end
end
defp set_pipeline(config) do
config
|> Map.put(:additional_pipeline, config.pipeline)
|> Map.put(:pipeline, {__MODULE__, :pipeline})
end
@doc false
def pipeline(config, opts) do
{module, fun} = config.additional_pipeline
apply(module, fun, [config, opts])
|> Absinthe.Pipeline.insert_after(
Absinthe.Phase.Document.CurrentOperation,
[
Absinthe.GraphiQL.Validation.NoSubscriptionOnHTTP
]
)
end
@spec select_mode(request :: Absinthe.Plug.Request.t()) ::
:start_interface | {:process, Absinthe.Plug.Request.t()}
defp select_mode(%{queries: [%Absinthe.Plug.Request.Query{document: nil}]}),
do: :start_interface
defp select_mode(request), do: {:process, request}
defp find_socket_path(conn, socket) do
if endpoint = conn.private[:phoenix_endpoint] do
Enum.find_value(endpoint.__sockets__, :error, fn
# Phoenix 1.4
{path, ^socket, _opts} -> {:ok, path}
# Phoenix <= 1.3
{path, ^socket} -> {:ok, path}
_ -> false
end)
else
:error
end
end
@render_defaults %{var_string: "", results: ""}
@spec render_interface(Plug.Conn.t(), :advanced | :simple | :playground, map()) ::
Plug.Conn.t()
defp render_interface(conn, interface, opts)
defp render_interface(conn, :simple, opts) do
opts = opts_with_default(opts)
graphiql_html(
opts[:query],
opts[:var_string],
opts[:result],
opts[:socket_url],
opts[:assets]
)
|> rendered(conn)
end
defp render_interface(conn, :advanced, opts) do
opts = opts_with_default(opts)
graphiql_workspace_html(
opts[:query],
opts[:var_string],
opts[:default_headers],
default_url(opts[:default_url]),
opts[:socket_url],
opts[:assets]
)
|> rendered(conn)
end
defp render_interface(conn, :playground, opts) do
opts = opts_with_default(opts)
graphiql_playground_html(
default_url(opts[:default_url]),
opts[:socket_url],
opts[:assets]
)
|> rendered(conn)
end
defp opts_with_default(opts) do
defaults = Map.put(@render_defaults, :query, opts[:default_query])
Map.merge(defaults, opts)
end
defp default_url(nil), do: "window.location.origin + window.location.pathname"
defp default_url(url), do: "'#{url}'"
@spec rendered(String.t(), Plug.Conn.t()) :: Plug.Conn.t()
defp rendered(html, conn) do
conn
|> put_resp_content_type("text/html")
|> send_resp(200, html)
end
defp js_escape(string) do
string
|> String.replace(~r/\n/, "\\n")
|> String.replace(~r/'/, "\\'")
end
defp handle_default_headers(config, conn) do
case get_config_val(config, :default_headers, conn) do
nil ->
Map.put(config, :default_headers, "[]")
val when is_map(val) ->
header_string =
val
|> Enum.map(fn {k, v} -> %{"name" => k, "value" => v} end)
|> config.json_codec.module.encode!(pretty: true)
Map.put(config, :default_headers, header_string)
val ->
raise "invalid default headers: #{inspect(val)}"
end
end
defp function_arity(module, fun) do
Enum.find([1, 0], nil, &function_exported?(module, fun, &1))
end
defp put_config_value(config, key, conn) do
case get_config_val(config, key, conn) do
nil ->
config
val when is_binary(val) ->
Map.put(config, key, val)
val ->
raise "invalid #{key}: #{inspect(val)}"
end
end
defp get_config_val(config, key, conn) do
case Map.get(config, key) do
{module, fun} when is_atom(fun) ->
case function_arity(module, fun) do
1 ->
apply(module, fun, [conn])
0 ->
apply(module, fun, [])
_ ->
raise "function for #{key}: {#{module}, #{fun}} is not exported with arity 1 or 0"
end
val ->
val
end
end
defp handle_socket_url(config, conn) do
config
|> put_config_value(:socket_url, conn)
|> normalize_socket_url(conn)
end
defp normalize_socket_url(%{socket_url: nil, socket: socket} = config, conn) do
url =
with {:ok, socket_path} <- find_socket_path(conn, socket) do
"`${protocol}//${window.location.host}#{socket_path}`"
else
_ -> "''"
end
%{config | socket_url: url}
end
defp normalize_socket_url(%{socket_url: url} = config, _) do
%{config | socket_url: "'#{url}'"}
end
end