diff --git a/lib/introspection.ex b/lib/introspection.ex new file mode 100644 index 0000000..cb0c08a --- /dev/null +++ b/lib/introspection.ex @@ -0,0 +1,12 @@ +defmodule Rajska.Introspection do + @moduledoc false + + alias Absinthe.Type + + @doc """ + Introspect the Absinthe Type to get the underlying object type + """ + def get_object_type(%Type.List{of_type: object_type}), do: get_object_type(object_type) + def get_object_type(%Type.NonNull{of_type: object_type}), do: get_object_type(object_type) + def get_object_type(object_type), do: object_type +end diff --git a/lib/middlewares/object_authorization.ex b/lib/middlewares/object_authorization.ex index 1d069db..ce6bbda 100644 --- a/lib/middlewares/object_authorization.ex +++ b/lib/middlewares/object_authorization.ex @@ -57,6 +57,7 @@ defmodule Rajska.ObjectAuthorization do Schema, Type } + alias Rajska.Introspection alias Type.{Custom, Scalar} def call(%Resolution{state: :resolved} = resolution, _config), do: resolution @@ -67,15 +68,11 @@ defmodule Rajska.ObjectAuthorization do defp authorize(type, fields, resolution) do type + |> Introspection.get_object_type() |> lookup_object(resolution.schema) |> authorize_object(fields, resolution) end - # When is a list, inspect object that composes the list. - defp lookup_object(%Type.List{of_type: object_type}, schema) do - lookup_object(object_type, schema) - end - defp lookup_object(object_type, schema) do Schema.lookup_type(schema, object_type) end diff --git a/lib/middlewares/object_scope_authorization.ex b/lib/middlewares/object_scope_authorization.ex index 9ec26ac..69f2f0b 100644 --- a/lib/middlewares/object_scope_authorization.ex +++ b/lib/middlewares/object_scope_authorization.ex @@ -68,6 +68,7 @@ defmodule Rajska.ObjectScopeAuthorization do """ alias Absinthe.{Blueprint, Phase, Type} + alias Rajska.Introspection use Absinthe.Phase @spec run(Blueprint.t() | Phase.Error.t(), Keyword.t()) :: {:ok, map} @@ -92,7 +93,7 @@ defmodule Rajska.ObjectScopeAuthorization do # Object defp result(%{fields: fields, emitter: %{schema_node: schema_node} = emitter} = result, context) do - type = get_object_type(schema_node.type) + type = Introspection.get_object_type(schema_node.type) scope = Type.meta(type, :scope) case is_authorized?(scope, result.root_value, context, type) do @@ -109,11 +110,6 @@ defmodule Rajska.ObjectScopeAuthorization do # Leafs defp result(result, _context), do: result - # When is a list, inspect object that composes the list. - defp get_object_type(%Type.List{of_type: object_type}), do: object_type - defp get_object_type(%Type.NonNull{of_type: object_type}), do: object_type - defp get_object_type(object_type), do: object_type - defp walk_result(fields, context, new_fields \\ []) defp walk_result([], _context, new_fields), do: Enum.reverse(new_fields) @@ -140,7 +136,7 @@ defmodule Rajska.ObjectScopeAuthorization do defp error(%{source_location: location, schema_node: %{type: type}}) do %Phase.Error{ phase: __MODULE__, - message: "Not authorized to access object #{get_object_type(type).identifier}", + message: "Not authorized to access object #{Introspection.get_object_type(type).identifier}", locations: [location] } end