Skip to content

Commit

Permalink
Apply interface name resolution to other code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
TannerRogalsky committed Jan 9, 2025
1 parent 6d30c39 commit 4ce7c76
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
24 changes: 15 additions & 9 deletions lib/absinthe/federation/schema/entity_union.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,24 @@ end
defimpl Absinthe.Federation.Schema.EntityUnion.Resolver, for: Any do
alias Absinthe.Adapter.LanguageConventions

def resolve_type(%struct_name{}, resolution) do
struct_name
|> Module.split()
|> List.last()
|> to_internal_name(resolution.adapter)
def resolve_type(%struct_name{} = data, resolution) do
typename =
struct_name
|> Module.split()
|> List.last()

inner_resolve_type(data, typename, resolution)
end

def resolve_type(%{__typename: typename} = data, resolution) do
inner_resolve_type(data, typename, resolution)
end

def resolve_type(%{"__typename" => typename} = data, resolution) do
inner_resolve_type(data, typename, resolution)
end

defp inner_resolve_type(data, typename, resolution) do
type = Absinthe.Schema.lookup_type(resolution.schema, typename)

if is_nil(type) do
Expand All @@ -94,10 +104,6 @@ defimpl Absinthe.Federation.Schema.EntityUnion.Resolver, for: Any do
end
end

def resolve_type(%{"__typename" => typename}, resolution) do
to_internal_name(typename, resolution.adapter)
end

defp to_internal_name(name, adapter) when is_nil(adapter) do
name
|> LanguageConventions.to_internal_name(:type)
Expand Down
28 changes: 28 additions & 0 deletions test/absinthe/federation/schema/entity_union_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ defmodule Absinthe.Federation.Schema.EntityUnionTest do
key_fields("id")
field :id, non_null(:id)

field :_resolve_reference, :shape do
resolve(fn _, %{id: id} = args, _ ->
case id do
"123" -> {:ok, args}
"321" -> {:ok, args}
_ -> {:error, "ID doesn't exist #{id}"}
end
end)
end

resolve_type fn
data, _ ->
case data do
Expand All @@ -235,13 +245,31 @@ defmodule Absinthe.Federation.Schema.EntityUnionTest do
field :id, non_null(:id)

interface :shape

field :_resolve_reference, :circle do
resolve(fn _, %{id: id} = args, _ ->
case id do
"123" -> {:ok, args}
_ -> {:error, "ID doesn't exist #{id}"}
end
end)
end
end

object :rectangle do
key_fields("id")
field :id, non_null(:id)

interface :shape

field :_resolve_reference, :rectangle do
resolve(fn _, %{id: id} = args, _ ->
case id do
"321" -> {:ok, args}
_ -> {:error, "ID doesn't exist #{id}"}
end
end)
end
end
end

Expand Down
4 changes: 0 additions & 4 deletions test/support/entity_interface/circle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ defmodule Example.EntityInterface.Circle do
}

defstruct id: ""

defimpl Absinthe.Federation.Schema.EntityUnion.Resolver do
def resolve_type(_, _), do: :circle
end
end
4 changes: 0 additions & 4 deletions test/support/entity_interface/rectangle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ defmodule Example.EntityInterface.Rectangle do
}

defstruct id: ""

defimpl Absinthe.Federation.Schema.EntityUnion.Resolver do
def resolve_type(_, _), do: :rectangle
end
end

0 comments on commit 4ce7c76

Please sign in to comment.