Skip to content

Commit 3aef283

Browse files
authored
feat: add GQL sigil to format and lint static GraphQL docs (#1391)
* add GQL sigil to format and lint static GraphQL docs particularly useful in tests * dialyzer was unhappy with my safety net * hush, dialyzer
1 parent 66cec34 commit 3aef283

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

lib/absinthe/formatter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule Absinthe.Formatter do
2626
"""
2727

2828
def features(_opts) do
29-
[sigils: [], extensions: [".graphql", ".gql"]]
29+
[sigils: [:GQL], extensions: [".graphql", ".gql"]]
3030
end
3131

3232
def format(contents, _opts \\ []) do

lib/absinthe/sigil.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule Absinthe.Sigil do
2+
@moduledoc """
3+
Provides the ~GQL sigil
4+
"""
5+
6+
def sigil_GQL(string, []) do
7+
with {:ok, blueprint} <- Absinthe.Phase.Parse.run(string, []) do
8+
inspect(blueprint.input, pretty: true)
9+
else
10+
{:error, %Absinthe.Blueprint{execution: %{validation_errors: [_ | _] = errors}}} ->
11+
{:current_stacktrace, [_process_info, _absinthe_sigil, {_, _, _, loc} | _]} =
12+
Process.info(self(), :current_stacktrace)
13+
14+
err_string =
15+
Enum.join(
16+
[
17+
"~GQL sigil validation error at " <> inspect(loc)
18+
| Enum.map(errors, &"#{&1.message} (#{inspect(&1.locations)})")
19+
],
20+
"\n"
21+
)
22+
23+
IO.puts(:stderr, err_string)
24+
string
25+
26+
other ->
27+
other
28+
end
29+
end
30+
end

test/absinthe/sigil_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule Absinthe.SigilTest do
2+
use Absinthe.Case, async: true
3+
import ExUnit.CaptureIO
4+
5+
test "~GQL sigil formatting" do
6+
assert ~GQL"{version}" == "{\n version\n}\n"
7+
end
8+
9+
test "~GQL sigil validation error reporting" do
10+
this_file = __ENV__.file |> String.split("/") |> List.last()
11+
# ENV gives full path, but error message is relative path
12+
13+
err_out =
14+
capture_io(:stderr, fn ->
15+
assert ~GQL"{version" == "{version"
16+
end)
17+
18+
assert err_out =~ "~GQL sigil validation error"
19+
assert err_out =~ this_file
20+
assert err_out =~ "syntax error"
21+
22+
err_out =
23+
capture_io(:stderr, fn ->
24+
assert ~GQL"query { item(this-won't-lex) }" == "query { item(this-won't-lex) }"
25+
end)
26+
27+
assert err_out =~ "~GQL sigil validation error"
28+
assert err_out =~ this_file
29+
assert err_out =~ "Parsing failed at `-won't-lex`"
30+
end
31+
end

test/support/case.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule Absinthe.Case do
88
import Absinthe.Case.Helpers.Run
99
import Absinthe.Case.Assertions.Result
1010
import Absinthe.Case.Assertions.Schema
11+
import Absinthe.Sigil
1112
end
1213
end
1314
end

0 commit comments

Comments
 (0)