Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when object is a nested Absinthe Type #10

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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