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

Default values not rendered in the sdl #1110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 48 additions & 1 deletion lib/absinthe/schema/notation/sdl_render.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,47 @@ defmodule Absinthe.Schema.Notation.SDL.Render do

@adapter Absinthe.Adapter.LanguageConventions
defp render(%Blueprint.Schema.InputValueDefinition{} = input_value, type_definitions) do
default_value =
case input_value.default_value do
nil ->
nil

value when is_atom(value) ->
typ =
case input_value.type do
%Absinthe.Blueprint.TypeReference.NonNull{of_type: t} -> t
%Absinthe.Blueprint.TypeReference.List{of_type: t} -> t
t -> t
end

definition = Enum.find(type_definitions, fn d -> typ == d.identifier end)

case definition do
nil ->
value

_ ->
enum = Absinthe.Blueprint.Schema.EnumTypeDefinition.build(definition, nil)

%Blueprint.Input.Enum{
value: Absinthe.Type.Enum.serialize(enum, value),
source_location: input_value.source_location
}
end

value ->
Blueprint.Input.parse(value)
end

concat([
string(@adapter.to_external_name(input_value.name, :argument)),
": ",
render(input_value.type, type_definitions),
default(input_value.default_value_blueprint),
default(input_value.default_value_blueprint || default_value),
directives(input_value.directives, type_definitions)
])
|> description(input_value.description)
|> deprecation(input_value.deprecation)
end

defp render(%Blueprint.Schema.FieldDefinition{} = field, type_definitions) do
Expand All @@ -122,6 +155,7 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
directives(field.directives, type_definitions)
])
|> description(field.description)
|> deprecation(field.deprecation)
end

defp render(%Blueprint.Schema.ObjectTypeDefinition{} = object_type, type_definitions) do
Expand Down Expand Up @@ -211,6 +245,7 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
directives(enum_value.directives, type_definitions)
])
|> description(enum_value.description)
|> deprecation(enum_value.deprecation)
end

defp render(%Blueprint.Schema.ScalarTypeDefinition{} = scalar_type, type_definitions) do
Expand Down Expand Up @@ -342,6 +377,10 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
empty()
end

defp default(%{value: nil}) do
empty()
end

defp default(default_value) do
concat([" = ", render_value(default_value)])
end
Expand All @@ -358,6 +397,14 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
])
end

defp deprecation(docs, nil) do
docs
end

defp deprecation(docs, %Absinthe.Type.Deprecation{reason: reason}) do
concat([docs, " @deprecated(reason: \"#{reason}\")"])
end

defp implements(%{interface_blueprints: [], interfaces: []}, _) do
empty()
end
Expand Down
14 changes: 11 additions & 3 deletions test/absinthe/schema/sdl_render_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ defmodule Absinthe.Schema.SdlRenderTest do

field :echo, :string do
arg :times, :integer, default_value: 10, description: "The number of times"
arg :time_interval, :integer
arg :time_interval, :integer, deprecate: "Old interval"
arg :time_string, :string, default_value: "2021"
arg :order_status, non_null(:order_status), default_value: :processing
end

field :search, :search_result
Expand All @@ -219,6 +221,7 @@ defmodule Absinthe.Schema.SdlRenderTest do
object :order do
field :id, :id
field :name, :string
field :braun, :string, deprecate: "Deprecated"
field :status, :order_status
field :other_status, :status
import_fields :imported_fields
Expand Down Expand Up @@ -251,9 +254,13 @@ defmodule Absinthe.Schema.SdlRenderTest do
type RootQueryType {
echo(
"The number of times"
times: Int
times: Int = 10

timeInterval: Int
timeInterval: Int @deprecated(reason: \"Old interval\")

timeString: String = "2021"

orderStatus: OrderStatus! = PROCESSING
): String
search: SearchResult
}
Expand All @@ -280,6 +287,7 @@ defmodule Absinthe.Schema.SdlRenderTest do
imported: Boolean!
id: ID
name: String
braun: String @deprecated(reason: \"Deprecated\")
status: OrderStatus
otherStatus: Status
}
Expand Down