Skip to content

Commit

Permalink
Fix bug when object is a nested Absinthe Type
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielpra1 committed Sep 19, 2019
1 parent 137be9c commit d55c610
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/introspection.ex
Original file line number Diff line number Diff line change
@@ -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
7 changes: 2 additions & 5 deletions lib/middlewares/object_authorization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions lib/middlewares/object_scope_authorization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit d55c610

Please sign in to comment.