Skip to content

Commit

Permalink
Ensure we render all possible types & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryseed committed Aug 17, 2018
1 parent 0471f13 commit 792fee5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
48 changes: 23 additions & 25 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -284,31 +284,6 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end
end

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

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

%Absinthe.Type.InputObject{fields: fields} ->
contents =
fields
|> Enum.map(fn {name, %{type: type}} ->
key = name |> to_string |> Absinthe.Utils.camelize(lower: true)
val = render_default_value(schema, type, value[name])
"#{key}: #{val}"
end)
|> Enum.join(", ")

"{#{contents}}"

_ ->
to_string(value)
end
end

object :__enumvalue, name: "__EnumValue" do
field :name, :string

Expand All @@ -334,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)
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} = thing ->
inspect(serializer.(value))
end
end
end
19 changes: 17 additions & 2 deletions test/absinthe/introspection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ defmodule Absinthe.IntrospectionTest do

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

Expand All @@ -214,6 +220,12 @@ defmodule Absinthe.IntrospectionTest do
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

Expand Down Expand Up @@ -243,7 +255,10 @@ defmodule Absinthe.IntrospectionTest do
"fields" => [
%{
"args" => [
%{"defaultValue" => "{fancyEnum: FOO, fancyValue: \"qwerty\"}"}
%{
"defaultValue" =>
"{fancyEnum: FOO, fancyList: [1, 2, 3], fancyNested: {fancyBool: false}, fancyValue: \"qwerty\"}"
}
]
}
]
Expand Down

0 comments on commit 792fee5

Please sign in to comment.