diff --git a/lib/schema.ex b/lib/schema.ex index 669b313..fee1d32 100644 --- a/lib/schema.ex +++ b/lib/schema.ex @@ -17,8 +17,8 @@ defmodule Rajska.Schema do Field.t(), module() ) :: [Middleware.spec(), ...] - def add_query_authorization(middleware, %Field{name: query_name}, authorization) do - middleware + def add_query_authorization(middlewares, %Field{name: query_name}, authorization) do + middlewares |> Enum.find(&find_middleware/1) |> case do {{QueryAuthorization, :call}, config} -> @@ -28,7 +28,7 @@ defmodule Rajska.Schema do raise "No permission specified for query #{query_name}" end - middleware + middlewares end def find_middleware({{QueryAuthorization, :call}, _config}), do: true @@ -36,12 +36,18 @@ defmodule Rajska.Schema do def find_middleware({_middleware, _config}), do: false @spec add_object_authorization([Middleware.spec(), ...]) :: [Middleware.spec(), ...] - def add_object_authorization([{{QueryAuthorization, :call}, _} = query_authorization | middleware]) do - [query_authorization, ObjectAuthorization] ++ middleware + def add_object_authorization(middlewares) do + middlewares + |> Enum.reduce([], fn + {{QueryAuthorization, :call}, _config} = query_authorization, new_middlewares -> + [ObjectAuthorization, query_authorization] ++ new_middlewares + + middleware, new_middlewares -> + [middleware | new_middlewares] + end) + |> Enum.reverse() end - def add_object_authorization(middleware), do: [ObjectAuthorization | middleware] - @spec add_field_authorization( [Middleware.spec(), ...], Field.t(),