-
Notifications
You must be signed in to change notification settings - Fork 529
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
benwilson512
merged 5 commits into
absinthe-graphql:master
from
maartenvanvliet:issues/convert_language_ast_to_string_representation
Nov 11, 2021
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2f36df2
Store if document uses shorthand notation
maartenvanvliet 85a1279
Render Absinthe.Language AST to graphql string
maartenvanvliet 9673a1c
Move common utils to separate file
maartenvanvliet f1a1499
Add Absinthe graphql formatter plugin
maartenvanvliet ced91e4
Separate docs with two newlines
maartenvanvliet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]] | ||
end | ||
|
||
def format(contents, _opts \\ []) do | ||
{:ok, blueprint} = Absinthe.Phase.Parse.run(contents, []) | ||
inspect(blueprint.input, pretty: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("\n") | ||
maartenvanvliet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.