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 5 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
34 changes: 24 additions & 10 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
{: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}

%{serialize: serializer} ->
{:ok, inspect(serializer.(value))}

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

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

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

"{#{object_values}}"

%Absinthe.Type.Field{type: type, name: name, identifier: identifier} ->
key = Absinthe.Utils.camelize(name, lower: true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final note here and then we can merge. Instead of doing the camelize call here, pull the adapter out of the resolution struct on line 279: _, %{schema: schema, source: %{default_value: value, type: type}, adapter: adapter} -> and then pass it in to this function. With that you should be able to do:

key = adapter.to_external_name(name, :field)

This way it will honor adapter settings if people are using custom ones.

val = render_default_value(schema, type, value[identifier])
"#{key}: #{val}"

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

%Absinthe.Type.Scalar{serialize: serializer} ->
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: [1, 2, 3]
}
end
end

enum :an_enum do
value :foo
value :bar
end

input_object :complex_input do
field :fancy_value, :string
field :fancy_enum, :an_enum
field :fancy_list, list_of(:integer)
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: [1, 2, 3], fancyNested: {fancyBool: false}, fancyValue: \"qwerty\"}"
}
]
}
]
}
}
}
}},
result
)
end
end

describe "introspection of an object type" do
Expand Down