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

Convert Absinthe.Language.* structs to graphql representation #1114

Merged
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
34 changes: 34 additions & 0 deletions lib/absinthe/formatter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Absinthe.Formatter do
@moduledoc """
Formatter task for graphql

Will format files with the extensions .graphql or .gql

## Example

Absinthe.Formatter.format("{ version }")
"{\n version\n}\n"


From Elixir 1.13 onwards the Absinthe.Formatter can be added to
the formatter as a plugin:

# .formatter.exs
[
# Define the desired plugins
plugins: [Absinthe.Formatter],
# Remember to update the inputs list to include the new extensions
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "{lib, priv}/**/*.{gql,graphql}"]
]

"""

def features(_opts) do
[sigils: [], extensions: [".graphql", ".gql"]]
Copy link
Contributor

@binaryseed binaryseed Nov 4, 2021

Choose a reason for hiding this comment

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

Is there a use case for a :G / :A sigil here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since Absinthe does not define a sigil of its own I've left it out here. It's fairly simple for users to define their own formatter plugin and include any custom sigils.

end

def format(contents, _opts \\ []) do
{:ok, blueprint} = Absinthe.Phase.Parse.run(contents, [])
inspect(blueprint.input, pretty: true)
end
end
5 changes: 5 additions & 0 deletions lib/absinthe/language/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ defmodule Absinthe.Language.Document do
update_in(blueprint.fragments, &[Blueprint.Draft.convert(node, doc) | &1])
end
end

defimpl Inspect do
defdelegate inspect(term, options),
to: Absinthe.Language.Render
end
end
2 changes: 2 additions & 0 deletions lib/absinthe/language/operation_definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Absinthe.Language.OperationDefinition do
variable_definitions: [],
directives: [],
selection_set: nil,
shorthand: false,
loc: %{line: nil}

@type t :: %__MODULE__{
Expand All @@ -16,6 +17,7 @@ defmodule Absinthe.Language.OperationDefinition do
variable_definitions: [Language.VariableDefinition.t()],
directives: [Language.Directive.t()],
selection_set: Language.SelectionSet.t(),
shorthand: boolean(),
loc: Language.loc_t()
}

Expand Down
279 changes: 279 additions & 0 deletions lib/absinthe/language/render.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
defmodule Absinthe.Language.Render do
@moduledoc false
import Inspect.Algebra
import Absinthe.Utils.Render

@line_width 120

def inspect(term, %{pretty: true}) do
term
|> render()
|> concat(line())
|> format(@line_width)
|> to_string
end

def inspect(term, options) do
Inspect.Any.inspect(term, options)
end

defp render(bp)

defp render(%Absinthe.Language.Document{} = doc) do
doc.definitions |> Enum.map(&render/1) |> join([line(), line()])
end

defp render(%Absinthe.Language.OperationDefinition{} = op) do
if op.shorthand do
concat(operation_definition(op), block(render_list(op.selection_set.selections)))
else
glue(
concat([to_string(op.operation), operation_definition(op)]),
block(render_list(op.selection_set.selections))
)
end
end

defp render(%Absinthe.Language.Field{} = field) do
case field.selection_set do
nil ->
field_definition(field)

selection_set ->
concat([
field_definition(field),
" ",
block(render_list(selection_set.selections))
])
end
end

defp render(%Absinthe.Language.VariableDefinition{} = variable_definition) do
concat([
"$",
variable_definition.variable.name,
": ",
render(variable_definition.type),
default_value(variable_definition)
])
end

defp render(%Absinthe.Language.NamedType{} = named_type) do
named_type.name
end

defp render(%Absinthe.Language.NonNullType{} = non_null) do
concat(render(non_null.type), "!")
end

defp render(%Absinthe.Language.Argument{} = argument) do
concat([argument.name, ": ", render(argument.value)])
end

defp render(%Absinthe.Language.Directive{} = directive) do
concat([" @", directive.name, arguments(directive.arguments)])
end

defp render(%Absinthe.Language.FragmentSpread{} = spread) do
concat(["...", spread.name, directives(spread.directives)])
end

defp render(%Absinthe.Language.InlineFragment{} = fragment) do
concat([
"...",
inline_fragment_name(fragment),
directives(fragment.directives),
" ",
block(render_list(fragment.selection_set.selections))
])
end

defp render(%Absinthe.Language.Variable{} = variable) do
concat("$", variable.name)
end

defp render(%Absinthe.Language.StringValue{value: value}) do
render_string_value(value)
end

defp render(%Absinthe.Language.FloatValue{value: value}) do
"#{value}"
end

defp render(%Absinthe.Language.ObjectField{} = object_field) do
concat([object_field.name, ": ", render(object_field.value)])
end

defp render(%Absinthe.Language.ObjectValue{fields: fields}) do
fields = fields |> Enum.map(&render(&1)) |> join(", ")

concat(["{ ", fields, " }"])
end

defp render(%Absinthe.Language.NullValue{}) do
"null"
end

defp render(%Absinthe.Language.ListType{type: type}) do
concat(["[", render(type), "]"])
end

defp render(%Absinthe.Language.ListValue{values: values}) do
values = values |> Enum.map(&render(&1)) |> join(", ")

concat(["[", values, "]"])
end

defp render(%Absinthe.Language.Fragment{} = fragment) do
concat([
"fragment ",
fragment.name,
" on ",
fragment.type_condition.name,
directives(fragment.directives)
])
|> block(render_list(fragment.selection_set.selections))
end

defp render(%{value: value}) do
to_string(value)
end

defp operation_definition(%{name: nil} = op) do
case op.variable_definitions do
[] ->
concat(
variable_definitions(op.variable_definitions),
directives(op.directives)
)

_ ->
operation_definition(%{op | name: ""})
end
end

defp operation_definition(%{name: name} = op) do
concat([" ", name, variable_definitions(op.variable_definitions), directives(op.directives)])
end

defp variable_definitions([]) do
empty()
end

defp variable_definitions(definitions) do
definitions = Enum.map(definitions, &render(&1))

concat([
"(",
join(definitions, ", "),
")"
])
end

defp field_definition(field) do
concat([
field_alias(field),
field.name,
arguments(field.arguments),
directives(field.directives)
])
end

defp default_value(%{default_value: nil}) do
empty()
end

defp default_value(%{default_value: value}) do
concat(" = ", render(value))
end

defp directives([]) do
empty()
end

defp directives(directives) do
directives |> Enum.map(&render(&1)) |> join(" ")
end

defp inline_fragment_name(%{type_condition: nil}) do
empty()
end

defp inline_fragment_name(%{type_condition: %{name: name}}) do
" on #{name}"
end

defp field_alias(%{alias: nil}) do
empty()
end

defp field_alias(%{alias: alias}) do
concat(alias, ": ")
end

defp arguments([]) do
empty()
end

defp arguments(args) do
group(
glue(
nest(
glue(
"(",
"",
render_list(args, ", ")
),
2,
:break
),
"",
")"
)
)
end

# Helpers

defp block(docs) do
do_block(docs)
end

defp block(:doc_nil, docs) do
do_block(docs)
end

defp block(name, docs) do
glue(
name,
do_block(docs)
)
end

defp do_block(docs) do
group(
glue(
nest(
force_unfit(
glue(
"{",
"",
docs
)
),
2,
:always
),
"",
"}"
)
)
end

defp render_list(items, separator \\ line()) do
List.foldr(items, :doc_nil, fn
item, :doc_nil -> render(item)
item, acc -> concat([render(item)] ++ [separator] ++ [acc])
end)
end
end
Loading