From 1352d8fd6e520dabae3cd642c69ec663065d829c Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Fri, 25 Dec 2020 12:10:17 -0800 Subject: [PATCH] All directives via directives --- lib/absinthe/schema/notation/sdl_render.ex | 76 +++++++------------ .../schema/type_system_directive_test.exs | 6 +- 2 files changed, 34 insertions(+), 48 deletions(-) diff --git a/lib/absinthe/schema/notation/sdl_render.ex b/lib/absinthe/schema/notation/sdl_render.ex index 52a78bc390..16c8881cfd 100644 --- a/lib/absinthe/schema/notation/sdl_render.ex +++ b/lib/absinthe/schema/notation/sdl_render.ex @@ -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 @@ -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 @@ -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 @@ -292,6 +288,8 @@ defmodule Absinthe.Schema.Notation.SDL.Render do end) end + # SDL Syntax Helpers + defp directives([], _) do empty() end @@ -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 ), @@ -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 @@ -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]) diff --git a/test/absinthe/schema/type_system_directive_test.exs b/test/absinthe/schema/type_system_directive_test.exs index f163786f57..7b29a2b602 100644 --- a/test/absinthe/schema/type_system_directive_test.exs +++ b/test/absinthe/schema/type_system_directive_test.exs @@ -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") { @@ -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