Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 62680c7

Browse files
committed
* GraphiQL 0.6.1
* Improving validation of functions
1 parent e654a7f commit 62680c7

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

lib/graphql/plug.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ defmodule GraphQL.Plug do
3535

3636
@spec init(Map) :: init
3737
def init(opts) do
38-
opts
39-
|> GraphQL.Plug.Endpoint.init
40-
|> GraphQL.Plug.GraphiQL.init
38+
graphiql = GraphQL.Plug.GraphiQL.init(opts)
39+
endpoint = GraphQL.Plug.Endpoint.init(opts)
40+
Enum.dedup(endpoint ++ graphiql)
4141
end
4242

4343
def call(conn, opts) do

lib/graphql/plug/configurable_value.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ defmodule GraphQL.Plug.ConfigurableValue do
99
@type t :: {module, atom} | (Plug.Conn.t -> Map) | Map | nil
1010
@spec evaluate(Plug.Conn.t, t, any) :: Map
1111

12+
@error_msg "Configured function must only be arity of 1 that accepts a value of Plug.Conn"
13+
1214
def evaluate(conn, {mod, func}, _) do
13-
apply(mod, func, [conn])
15+
if :erlang.function_exported(mod, func, 1) do
16+
apply(mod, func, [conn])
17+
else
18+
raise @error_msg
19+
end
1420
end
1521

1622
def evaluate(conn, root_fn, _) when is_function(root_fn, 1) do
1723
apply(root_fn, [conn])
1824
end
1925

2026
def evaluate(_, root_fn, _) when is_function(root_fn) do
21-
raise "Configured function must only be arity of 1 that accepts a value of Plug.Conn"
27+
raise @error_msg
2228
end
2329

2430
def evaluate(_, nil, default) do

lib/graphql/plug/endpoint.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ defmodule GraphQL.Plug.Endpoint do
4141
end
4242

4343
def call(%Conn{method: m} = conn, opts) when m in ["GET", "POST"] do
44-
# %{schema: schema, root_value: root_value, query: query} = opts
4544
root_value = opts[:root_value]
4645
schema = opts[:schema]
4746
query = opts[:query]

lib/graphql/plug/graphiql.ex

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
defmodule GraphQL.Plug.GraphiQL do
22
@moduledoc """
3-
This is the core plug for mounting a GraphQL server.
3+
This is the GraphiQL plug for mounting a GraphQL server.
44
55
You can build your own pipeline by mounting the
6-
`GraphQL.Plug.Endpoint` plug directly.
6+
`GraphQL.Plug.GraphiQL` plug directly.
77
88
```elixir
9-
forward "/graphql", GraphQL.Plug.Endpoint, schema: {MyApp.Schema, :schema}
9+
forward "/graphql", GraphQL.Plug.GraphiQL, schema: {MyApp.Schema, :schema}
1010
```
1111
1212
You may want to look at how `GraphQL.Plug` configures its pipeline.
1313
Specifically note how `Plug.Parsers` are configured, as this is required
1414
for pre-parsing the various POST bodies depending on `content-type`.
15-
16-
This plug currently includes _GraphiQL_ support but this should end
17-
up in it's own plug.
1815
"""
1916
import Plug.Conn
2017
alias Plug.Conn
@@ -24,7 +21,7 @@ defmodule GraphQL.Plug.GraphiQL do
2421

2522
@behaviour Plug
2623

27-
@graphiql_version "0.4.9"
24+
@graphiql_version "0.6.1"
2825
@graphiql_instructions """
2926
# Welcome to GraphQL Elixir!
3027
#
@@ -48,7 +45,7 @@ defmodule GraphQL.Plug.GraphiQL do
4845
[:graphiql_version, :query, :variables, :result]
4946

5047
def init(opts) do
51-
allow_graphiql? = Keyword.get(opts, :allow_graphiql?, false)
48+
allow_graphiql? = Keyword.get(opts, :allow_graphiql?, true)
5249

5350
GraphQL.Plug.Endpoint.init(opts) ++ [allow_graphiql?: allow_graphiql?]
5451
end
@@ -97,9 +94,8 @@ defmodule GraphQL.Plug.GraphiQL do
9794
|> send_resp(200, graphiql)
9895
end
9996

100-
def use_graphiql?(%Conn{method: "GET"}, %{allow_graphiql?: false}), do: false
101-
def use_graphiql?(%Conn{method: "GET"} = conn, %{allow_graphiql?: true}) do
102-
case get_req_header(conn, "accept") do
97+
def use_graphiql?(%Conn{method: "GET"} = conn, opts) do
98+
case opts[:allow_graphiql?] && get_req_header(conn, "accept") do
10399
[accept_header | _] ->
104100
String.contains?(accept_header, "text/html") &&
105101
!Map.has_key?(conn.params, "raw")

test/graphql/plug/configurable_value_test.exs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ defmodule GraphQL.Plug.ConfigurableValueTest do
4242
end
4343

4444
test "raises error when {mod, fun} is not arity/1" do
45-
# TODO: This should have a better error messsage as this is not useful
4645
assert_raise(
47-
UndefinedFunctionError,
48-
"undefined function GraphQL.Plug.ConfigurableValueTest.Config.bar/1",
46+
RuntimeError,
47+
"Configured function must only be arity of 1 that accepts a value of Plug.Conn",
4948
fn ->
5049
%{}
5150
|> ConfigurableValue.evaluate({Config, :bar}, :default)

0 commit comments

Comments
 (0)