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 SDL generation for interfaces #979

Merged
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
13 changes: 9 additions & 4 deletions lib/absinthe/schema/notation/sdl_render.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
"type",
concat([
string(object_type.name),
implements(object_type.interface_blueprints)
binaryseed marked this conversation as resolved.
Show resolved Hide resolved
implements(object_type.interfaces, type_definitions)
]),
render_list(object_type.fields, type_definitions)
)
Expand Down Expand Up @@ -364,12 +364,17 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
|> concat()
end

defp implements([]) do
defp implements([], _) do
empty()
end

defp implements(interface_types) do
interface_names = Enum.map(interface_types, & &1.name)
defp implements(interface_identifiers, type_definitions) do
interface_names =
Enum.map(interface_identifiers, fn interface_identifier ->
Enum.find_value(type_definitions, fn type ->
if interface_identifier == type.identifier, do: type.name
end)
end)

concat([
" implements ",
Expand Down
39 changes: 39 additions & 0 deletions test/mix/tasks/absinthe.schema.sdl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ defmodule Mix.Tasks.Absinthe.Schema.SdlTest do

type Query {
helloWorld(name: String!): String
interfaceField: Being
}

interface Being {
name: String
}

type Human implements Being {
name: String
}

type Robot implements Being {
name: String
}
"""
|> import_sdl

def hydrate(%Absinthe.Blueprint.Schema.InterfaceTypeDefinition{}, _) do
{:resolve_type, &__MODULE__.resolve_type/1}
end

def hydrate(_node, _ancestors), do: []

def resolve_type(_), do: false
end

@test_schema "Mix.Tasks.Absinthe.Schema.SdlTest.TestSchema"
Expand Down Expand Up @@ -63,10 +84,27 @@ defmodule Mix.Tasks.Absinthe.Schema.SdlTest do
field :hello_world, :mod do
arg :name, non_null(:string)
end

field :interface_field, :being
end

object :mod do
end

interface :being do
field :name, :string
resolve_type(fn obj, _ -> obj.type end)
end

object :human do
interface :being
field :name, :string
end

object :robot do
interface :being
field :name, :string
end
end

@test_mod_schema "Mix.Tasks.Absinthe.Schema.SdlTest.TestSchemaWithMods"
Expand Down Expand Up @@ -111,6 +149,7 @@ defmodule Mix.Tasks.Absinthe.Schema.SdlTest do

assert schema =~ "type Mod {"
assert schema =~ "modField: String"
assert schema =~ "type Robot implements Being"
end
end
end