diff --git a/lib/graphql/plug/endpoint.ex b/lib/graphql/plug/endpoint.ex index cd13a5c..3fa3f5e 100644 --- a/lib/graphql/plug/endpoint.ex +++ b/lib/graphql/plug/endpoint.ex @@ -55,7 +55,10 @@ defmodule GraphQL.Plug.Endpoint do end def call(%Conn{method: m} = conn, opts) when m in ["GET", "POST"] do - %{schema: schema, root_value: root_value} = conn.assigns[:graphql_options] || opts + if conn.assigns[:graphql_options] do + opts = Map.merge(opts, conn.assigns[:graphql_options]) + end + %{schema: schema, root_value: root_value} = opts query = query(conn) variables = variables(conn) diff --git a/test/graphql/plug/endpoint_test.exs b/test/graphql/plug/endpoint_test.exs index ad7e756..e7047df 100644 --- a/test/graphql/plug/endpoint_test.exs +++ b/test/graphql/plug/endpoint_test.exs @@ -23,8 +23,9 @@ defmodule GraphQL.Plug.EndpointTest do } end - def greeting(_, %{name: name}, _), do: "Hello, #{name}!" def greeting(%{greeting: name}, _, _), do: "Hello, #{name}!" + def greeting(_, %{name: name}, _), do: "Hello, #{name}!" + def greeting(_, _, %{root_value: %{name: name}}), do: "Hello, #{name}!" def greeting(_, _, _), do: greeting(%{}, %{name: "world"}, %{}) end @@ -72,6 +73,26 @@ defmodule GraphQL.Plug.EndpointTest do assert_query TestRootPlugWithMF, {:get, "/", query: "{greeting}"}, {200, success} end + test "root data can be set in conn and made available in resolve context" do + defmodule TestRootPlugWithConn do + use Plug.Builder + defmodule ConnTest do + import Plug.Conn + def init(default), do: default + + def call(conn, _default) do + assign(conn, :graphql_options, %{ root_value: %{ name: "Conn" } }) + end + end + + plug ConnTest + plug GraphQL.Plug.Endpoint, [schema: {TestSchema, :schema}] + end + + success = ~S({"data":{"greeting":"Hello, Conn!"}}) + assert_query TestRootPlugWithConn, {:get, "/", query: "{greeting}"}, {200, success} + end + test "GET with variables" do success = ~S({"data":{"greeting":"Hello, Josh!"}}) query = "query hi($name: String) { greeting(name: $name) }"