From 4cc4462a7c7f2e72ca9e0b0f112cdb96ad5407c6 Mon Sep 17 00:00:00 2001 From: Alex Martsinovich Date: Fri, 9 Dec 2022 09:59:45 -0800 Subject: [PATCH] Ensure module is loaded when fetching default_url --- lib/absinthe/plug/graphiql.ex | 2 ++ test/lib/absinthe/graphiql_test.exs | 19 +++++++++++++++++++ test/support/graphiql_default_url.ex | 3 +++ 3 files changed, 24 insertions(+) create mode 100644 test/support/graphiql_default_url.ex diff --git a/lib/absinthe/plug/graphiql.ex b/lib/absinthe/plug/graphiql.ex index ecd1e12..31c8632 100644 --- a/lib/absinthe/plug/graphiql.ex +++ b/lib/absinthe/plug/graphiql.ex @@ -437,6 +437,8 @@ defmodule Absinthe.Plug.GraphiQL do defp get_config_val(config, key, conn) do case Map.get(config, key) do {module, fun} when is_atom(fun) -> + Code.ensure_loaded(module) + case function_arity(module, fun) do 1 -> apply(module, fun, [conn]) diff --git a/test/lib/absinthe/graphiql_test.exs b/test/lib/absinthe/graphiql_test.exs index d330f7f..27e71d8 100644 --- a/test/lib/absinthe/graphiql_test.exs +++ b/test/lib/absinthe/graphiql_test.exs @@ -165,6 +165,25 @@ defmodule Absinthe.Plug.GraphiQLTest do ) end + test "default_url module is loaded" do + module = "Elixir.GraphiQLDefaultUrl" |> String.to_atom() + + opts = + Absinthe.Plug.GraphiQL.init( + schema: TestSchema, + default_url: {module, :graphiql_default_url} + ) + + assert %{status: status, resp_body: body} = + conn(:get, "/") + |> plug_parser + |> put_req_header("accept", "text/html") + |> Absinthe.Plug.GraphiQL.call(opts) + + assert 200 == status + assert String.contains?(body, "defaultUrl: '#{graphiql_default_url()}'") + end + test "socket_url option works a function of arity 0" do opts = Absinthe.Plug.GraphiQL.init( diff --git a/test/support/graphiql_default_url.ex b/test/support/graphiql_default_url.ex new file mode 100644 index 0000000..066d987 --- /dev/null +++ b/test/support/graphiql_default_url.ex @@ -0,0 +1,3 @@ +defmodule GraphiQLDefaultUrl do + def graphiql_default_url(), do: "https://api.foobarbaz.test" +end