diff --git a/lib/schema.ex b/lib/schema.ex index 560609a..a5f7ff3 100644 --- a/lib/schema.ex +++ b/lib/schema.ex @@ -12,12 +12,15 @@ defmodule Rajska.Schema do QueryAuthorization } + @modules_to_skip [Absinthe.Phase.Schema.Introspection] + @spec add_query_authorization( [Middleware.spec(), ...], Field.t(), module() ) :: [Middleware.spec(), ...] - def add_query_authorization(middlewares, %Field{name: query_name}, authorization) do + def add_query_authorization(middlewares, %Field{name: query_name, definition: definition_module}, authorization) + when definition_module not in @modules_to_skip do middlewares |> Enum.find(&find_middleware/1) |> case do @@ -31,6 +34,8 @@ defmodule Rajska.Schema do middlewares end + def add_query_authorization(middlewares, _field, _authorization), do: middlewares + def find_middleware({{QueryAuthorization, :call}, _config}), do: true def find_middleware({{Absinthe.Resolution, :call}, _config}), do: true def find_middleware({_middleware, _config}), do: false diff --git a/mix.exs b/mix.exs index 2a166a6..8d4623c 100644 --- a/mix.exs +++ b/mix.exs @@ -51,7 +51,7 @@ defmodule Rajska.MixProject do [ {:ex_doc, "~> 0.19", only: :dev, runtime: false}, {:credo, "~> 1.5.0", only: [:dev, :test], runtime: false}, - {:absinthe, "~> 1.4.0 or ~> 1.5.4"}, + {:absinthe, "~> 1.4.0 or ~> 1.5.4 or ~> 1.6.0"}, {:excoveralls, "~> 0.11", only: :test}, {:hammer, "~> 6.0", optional: true}, {:mock, "~> 0.3.0", only: :test}, diff --git a/mix.lock b/mix.lock index bc5c1d5..519e3e1 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,5 @@ %{ - "absinthe": {:hex, :absinthe, "1.5.4", "e84820f770149ada718e2bd58939322965089a5badd9a0bfe632e05b27248dd0", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6df329a071865065edd13a5a1da9981dfe8dc22c5d6729218ca9023999e9a2ff"}, + "absinthe": {:hex, :absinthe, "1.6.4", "d2958908b72ce146698de8ccbc03622630471eb0e354e06823aaef183e5067bd", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6e9c1cf36d86c704cb9a9c78db62d1c2676b03e0f61a28a23fc42749e8cd41ae"}, "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, "certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"}, "credo": {:hex, :credo, "1.5.1", "4fe303cc828412b9d21eed4eab60914c401e71f117f40243266aafb66f30d036", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0b219ca4dcc89e4e7bc6ae7e6539c313e738e192e10b85275fa1e82b5203ecd7"}, @@ -22,6 +22,6 @@ "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, "poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm", "dad79704ce5440f3d5a3681c8590b9dc25d1a561e8f5a9c995281012860901e3"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, - "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"}, + "telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"}, } diff --git a/test/middlewares/schema_test.exs b/test/middlewares/schema_test.exs index a1c4ec1..1854495 100644 --- a/test/middlewares/schema_test.exs +++ b/test/middlewares/schema_test.exs @@ -253,6 +253,46 @@ defmodule Rajska.SchemaTest do end end + test "Does not break and skips middleware check for subscriptions" do + defmodule SchemaWithSubscription do + use Absinthe.Schema + + def middleware(middleware, field, %{identifier: identifier}) + when identifier in [:query, :mutation] do + Rajska.add_query_authorization(middleware, field, Authorization) + end + + def middleware(middleware, _field, _object), do: middleware + + object :user do + field :email, :string + field :name, :string + end + + mutation do + field :create_user, :user do + middleware Rajska.QueryAuthorization, permit: :user, scope: false + resolve fn _args, _info -> {:ok, %{email: "email", name: "name"}} end + end + end + + query do + field :get_user, :user do + middleware Rajska.QueryAuthorization, permit: :user, scope: false + resolve fn _args, _info -> {:ok, %{email: "email", name: "name"}} end + end + end + + subscription do + field :new_users, :user do + arg :email, non_null(:string) + + config fn args, _info -> {:ok, topic: args.email} end + end + end + end + end + test "Adds object authorization after query authorization" do defmodule SchemaQueryAndObjectAuthorization do use Absinthe.Schema