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 complex default value InputType introspection #537

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
46 changes: 35 additions & 11 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,8 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
_, %{source: %{default_value: nil}} ->
{:ok, nil}

_, %{schema: schema, source: %{default_value: value, type: type}} ->
case Absinthe.Schema.lookup_type(schema, type, unwrap: true) do
%Absinthe.Type.Enum{values_by_internal_value: values} ->
{:ok, values[value].name}

%Absinthe.Type.Scalar{} = type ->
{:ok, inspect(Absinthe.Type.function(type, :serialize).(value))}

_ ->
{:ok, to_string(value)}
end
_, %{schema: schema, source: %{default_value: value, type: type}, adapter: adapter} ->
{:ok, render_default_value(schema, adapter, type, value)}

_, %{source: _} ->
{:ok, nil}
Expand Down Expand Up @@ -317,4 +308,37 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
{:ok, dep.reason}
end
end

def render_default_value(schema, adapter, type, value) do
case Absinthe.Schema.lookup_type(schema, type, unwrap: false) do
%Absinthe.Type.InputObject{fields: fields} ->
object_values =
Map.values(fields)
|> Enum.map(&render_default_value(schema, adapter, &1, value))
|> Enum.join(", ")

"{#{object_values}}"

%Absinthe.Type.List{of_type: type} ->
list_values =
Enum.map(value, &render_default_value(schema, adapter, type, &1))
|> Enum.join(", ")

"[#{list_values}]"

%Absinthe.Type.Field{type: type, name: name, identifier: identifier} ->
key = adapter.to_external_name(name, :field)
val = render_default_value(schema, adapter, type, value[identifier])
"#{key}: #{val}"

%Absinthe.Type.Enum{values_by_internal_value: values} ->
values[value].name

%Absinthe.Type.NonNull{of_type: type} ->
render_default_value(schema, adapter, type, value)

%Absinthe.Type.Scalar{serialize: serializer} = sc ->
inspect(serializer.(value))
end
end
end
73 changes: 73 additions & 0 deletions test/absinthe/introspection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,79 @@ defmodule Absinthe.IntrospectionTest do

assert !match?({:ok, %{data: %{"__type" => %{"fields" => _}}}}, result)
end

defmodule ComplexDefaultSchema do
use Absinthe.Schema

query do
field :complex_default, :string do
arg :input, :complex_input,
default_value: %{
fancy_value: "qwerty",
fancy_nested: %{fancy_bool: false},
fancy_enum: :foo,
fancy_list: [:foo, :bar]
}
end
end

enum :an_enum do
value :foo
value :bar
end

input_object :complex_input do
field :fancy_value, :string
field :fancy_enum, non_null(:an_enum)
field :fancy_list, list_of(:an_enum)
field :fancy_nested, :nested_complex_input
end

input_object :nested_complex_input do
field :fancy_bool, :boolean
end
end

test "can introspect complex default_vaule" do
result =
"""
{
__schema {
queryType {
fields {
args {
defaultValue
}
}
}
}
}
"""
|> run(ComplexDefaultSchema)

assert_result(
{:ok,
%{
data: %{
"__schema" => %{
"queryType" => %{
"fields" => [
%{
"args" => [
%{
"defaultValue" =>
"{fancyEnum: FOO, fancyList: [FOO, BAR], fancyNested: {fancyBool: false}, fancyValue: \"qwerty\"}"
}
]
}
]
}
}
}
}},
result
)
end
end

describe "introspection of an object type" do
Expand Down