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

Configure formatter width #1156

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions lib/absinthe/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ defmodule Absinthe.Formatter do
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "{lib, priv}/**/*.{gql,graphql}"]
]
```

## Options

- `:width` - desired max width
"""

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

def format(contents, _opts \\ []) do
def format(contents, opts \\ []) do
{:ok, blueprint} = Absinthe.Phase.Parse.run(contents, [])
inspect(blueprint.input, pretty: true)
inspect(blueprint.input, [{:pretty, true} | opts])
end
end
6 changes: 2 additions & 4 deletions lib/absinthe/schema/notation/sdl_render.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ defmodule Absinthe.Schema.Notation.SDL.Render do

alias Absinthe.Blueprint

@line_width 120

def inspect(term, %{pretty: true}) do
def inspect(term, %{pretty: true, width: width}) do
Copy link
Contributor

Choose a reason for hiding this comment

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

This will only match if a width is passed as an opt right? Do you also need another clause that does not match on width?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe we benefit here from the default Inspect.Opts.

term
|> render()
|> concat(line())
|> format(@line_width)
|> format(width)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not be applied in the Absinthe.Language.Render module? You add the width to the Absinthe.Formatter but this is never passed on to the SDL.Render module.

There are two intermedate representations in Absinthe of schema/executable documents. The Language structs, which is the result of the lexer/parser. And the Blueprint structs, which is used to build schema's and execute them.

Both representations can be rendered to the graphql string representation. The Language representation can be converted to executable documents.
Blueprints can be converted back to the schema definition language.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I overlooked that. I'm happy to make the change wherever it makes the most sense to you 😄 .

Copy link
Contributor

Choose a reason for hiding this comment

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

|> format(@line_width)
this is probably what you're looking for.

|> to_string
end

Expand Down
18 changes: 18 additions & 0 deletions test/absinthe/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,22 @@ defmodule Absinthe.FormatterTest do
test "formats a document" do
assert Absinthe.Formatter.format(@query) == "{\n version\n}\n"
end

@query """
query AQuery {
aVeryLongLineThatExceedsOneHundredAndTwentyCharactersAsWasTheDefault(thereAreMultipleArgsAsWell: ["A"], hereIsAnother: true) { id name }
}
"""
test "creates line breaks at 80 width by default" do
assert Absinthe.Formatter.format(@query) == """
query AQuery {
aVeryLongLineThatExceedsOneHundredAndTwentyCharactersAsWasTheDefault(
thereAreMultipleArgsAsWell: ["A"], hereIsAnother: true
) {
id
name
}
}
"""
end
end
11 changes: 9 additions & 2 deletions test/absinthe/schema/sdl_render_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule Absinthe.Schema.SdlRenderTest do
defaultListArg(things: [String] = ["ThisThing"]): [String]
defaultEnumArg(category: Category = NEWS): Category
defaultNullStringArg(name: String = null): String
aVeryLongLineThatExceedsOneHundredAndTwentyCharactersAsWasTheDefault(thereAreMultipleArgsAsWell: [String!]!, hereIsAnother: Boolean!): String!
animal: Animal
}

Expand Down Expand Up @@ -112,8 +113,14 @@ defmodule Absinthe.Schema.SdlRenderTest do
end

test "Render SDL from blueprint defined with SDL" do
assert Absinthe.Schema.to_sdl(SdlTestSchema) ==
SdlTestSchema.sdl()
sdl_a =
SdlTestSchema
|> Absinthe.Schema.to_sdl()
|> String.replace(~r/[\n ]/, "")

sdl_b = String.replace(SdlTestSchema.sdl(), ~r/[\n ]/, "")

assert sdl_a == sdl_b
end

describe "Render SDL" do
Expand Down