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

Commit 9af1046

Browse files
author
Josh Price
committed
Allow schema to be specified as {mod, fun}
This fixes an “invalid quoted expression” issue with macro expansion when using this syntax: get “/“, Plug, schema: Schema.schema but we can use: get “/“, Plug, schema: {Schema, :schema}
1 parent ddba0a0 commit 9af1046

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/graphql/plug/endpoint.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ defmodule GraphQL.Plug.Endpoint do
55
@behaviour Plug
66

77
def init(opts) do
8-
opts
8+
schema = case Keyword.get(opts, :schema) do
9+
{mod, func} -> apply(mod, func, [])
10+
s -> s
11+
end
12+
%{schema: schema}
913
end
1014

11-
def call(%Conn{method: req_method, params: %{"query" => query}} = conn, opts)
15+
def call(%Conn{method: req_method, params: %{"query" => query}} = conn, %{schema: schema})
1216
when req_method in ["GET", "POST"] do
13-
schema = opts[:schema]
1417
cond do
1518
query && String.strip(query) != "" -> handle_call(conn, schema, query)
1619
true -> handle_error(conn, "Must provide query string.")

test/graphql/plug/endpoint_test.exs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ defmodule GraphQL.Plug.EndpointTest do
2222
def greeting(_, _, _), do: greeting(%{}, %{name: "world"}, %{})
2323
end
2424

25-
# Setup a Plug which calls the Plug under test
2625
defmodule TestPlug do
2726
use Plug.Builder
2827

@@ -39,7 +38,6 @@ defmodule GraphQL.Plug.EndpointTest do
3938
assert conn.status == status
4039
assert get_resp_header(conn, "content-type") == ["application/json; charset=utf-8"]
4140
assert conn.resp_body == body
42-
# assert conn.halted == true
4341
end
4442

4543
test "GET and POST successful query" do
@@ -48,6 +46,20 @@ defmodule GraphQL.Plug.EndpointTest do
4846
assert_query {:post, "/", query: "{greeting}"}, {200, success}
4947
end
5048

49+
test "specify schema using {module, fun} syntax" do
50+
defmodule TestMFPlug do
51+
use Plug.Builder
52+
53+
plug GraphQL.Plug.Endpoint, schema: {TestSchema, :schema}
54+
end
55+
conn = conn(:get, "/", query: "{greeting}")
56+
conn = TestMFPlug.call conn, []
57+
58+
assert conn.status == 200
59+
assert get_resp_header(conn, "content-type") == ["application/json; charset=utf-8"]
60+
assert conn.resp_body == ~S({"data":{"greeting":"Hello, world!"}})
61+
end
62+
5163
test "missing query" do
5264
no_query_found_error = ~S({"errors":[{"message":"Must provide query string."}]})
5365
assert_query {:get, "/", nil}, {400, no_query_found_error}

0 commit comments

Comments
 (0)