Skip to content

Commit

Permalink
All directives via directives
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryseed committed Dec 25, 2020
1 parent b59d0d4 commit 1352d8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
76 changes: 29 additions & 47 deletions lib/absinthe/schema/notation/sdl_render.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
end

defp render(%Blueprint.Schema.FieldDefinition{} = field, type_definitions) do
directives = Enum.reject(field.directives, &(&1.name == "deprecated"))

concat([
string(@adapter.to_external_name(field.name, :field)),
arguments(field.arguments, type_definitions),
": ",
render(field.type, type_definitions),
directives(directives, type_definitions)
directives(field.directives, type_definitions)
])
|> deprecated(field.deprecation)
|> description(field.description)
end

Expand Down Expand Up @@ -196,7 +193,6 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
string(enum_value.name),
directives(enum_value.directives, type_definitions)
])
|> deprecated(enum_value.deprecation)
|> description(enum_value.description)
end

Expand Down Expand Up @@ -235,7 +231,7 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
concat([
argument.name,
": ",
render_default(argument.input_value.normalized)
render_value(argument.input_value.normalized)
])
end

Expand Down Expand Up @@ -292,6 +288,8 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
end)
end

# SDL Syntax Helpers

defp directives([], _) do
empty()
end
Expand Down Expand Up @@ -346,64 +344,46 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
end

defp default(default_value) do
concat([" = ", render_default(default_value)])
concat([" = ", render_value(default_value)])
end

defp render_default(%Blueprint.Input.String{value: value}),
do: ~s("#{value}")
defp render_value(%Blueprint.Input.String{value: value}),
do: render_string_value(value)

defp render_default(%Blueprint.Input.RawValue{content: content}),
do: render_default(content)
defp render_value(%Blueprint.Input.RawValue{content: content}),
do: render_value(content)

defp render_default(%Blueprint.Input.Value{raw: raw}),
do: render_default(raw)
defp render_value(%Blueprint.Input.Value{raw: raw}),
do: render_value(raw)

defp render_default(%Blueprint.Input.Object{fields: fields}) do
default_fields = Enum.map(fields, &render_default/1)
defp render_value(%Blueprint.Input.Object{fields: fields}) do
default_fields = Enum.map(fields, &render_value/1)
concat(["{", join(default_fields, ", "), "}"])
end

defp render_default(%Blueprint.Input.List{items: items}) do
default_list = Enum.map(items, &render_default/1)
defp render_value(%Blueprint.Input.List{items: items}) do
default_list = Enum.map(items, &render_value/1)
concat(["[", join(default_list, ", "), "]"])
end

defp render_default(%Blueprint.Input.Field{name: name, input_value: value}),
do: "#{name}: #{render_default(value)}"
defp render_value(%Blueprint.Input.Field{name: name, input_value: value}),
do: concat([name, ": ", render_value(value)])

defp render_default(%{value: value}),
defp render_value(%{value: value}),
do: to_string(value)

defp deprecated(docs, nil) do
docs
end

defp deprecated(docs, %{reason: nil}) do
space(docs, "@deprecated")
end

defp deprecated(docs, %{reason: reason}) do
concat([
space(docs, "@deprecated"),
"(",
"reason: ",
deprecated_reason(reason),
")"
])
end

defp deprecated_reason(reason) do
reason
defp render_string_value(str) do
str
|> String.trim()
|> String.split("\n")
|> case do
[reason] ->
concat([~s("), reason, ~s(")])
[str_line] ->
concat([~s("), str_line, ~s(")])

reason_lines ->
str_lines ->
concat(
nest(
block_string([~s(""")] ++ reason_lines),
block_string([~s(""")] ++ str_lines),
2,
:always
),
Expand Down Expand Up @@ -455,6 +435,11 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
])
end

defp repeatable(true), do: " repeatable"
defp repeatable(_), do: empty()

# Algebra Helpers

defp multiline(docs, true) do
force_unfit(docs)
end
Expand Down Expand Up @@ -507,9 +492,6 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
defp block_string_line(["", _ | _]), do: nest(line(), :reset)
defp block_string_line(_), do: line()

defp repeatable(true), do: " repeatable"
defp repeatable(_), do: empty()

def join(docs, joiner) do
fold_doc(docs, fn doc, acc ->
concat([doc, concat(List.wrap(joiner)), acc])
Expand Down
6 changes: 5 additions & 1 deletion test/absinthe/schema/type_system_directive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ defmodule TypeSystemDirectiveTest do
}
interface Animal @feature(name: ":interface") {
legCount: Int!
legCount: Int! @feature(name: \"""
Multiline here?
Second line
\""")
}
input SearchFilter @feature(name: ":input_object") {
Expand Down Expand Up @@ -71,6 +74,7 @@ defmodule TypeSystemDirectiveTest do
enum Category @feature(name: ":enum") {
THIS
THAT @feature(name: ":enum_value")
THE_OTHER @deprecated(reason: "It's old")
}
union SearchResult @feature(name: ":union") = Dog | Post
Expand Down

0 comments on commit 1352d8f

Please sign in to comment.