-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add field suggestions phase and tests
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
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,28 @@ | ||
defmodule AbsintheSecurity.Phase.DisableFieldSuggestions do | ||
def run(blueprint, options) do | ||
if Keyword.get(options, :enable_field_suggestions, false) do | ||
{:ok, blueprint} | ||
else | ||
do_run(blueprint) | ||
end | ||
end | ||
|
||
defp do_run(blueprint) do | ||
result = | ||
case blueprint.result do | ||
%{errors: errors} -> %{blueprint.result | errors: remove_field_suggestions(errors)} | ||
_ -> blueprint.result | ||
end | ||
|
||
{:ok, %{blueprint | result: result}} | ||
end | ||
|
||
def remove_field_suggestions(errors) do | ||
Enum.map(errors, fn error -> | ||
case Regex.run(~r/ Did you mean.+/, error.message) do | ||
[match] -> Map.put(error, :message, String.replace(error.message, match, "")) | ||
_ -> error | ||
end | ||
end) | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
test/absinthe_security/phase/disable_field_suggestions_test.exs
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,52 @@ | ||
defmodule AbsintheSecurity.Phase.DisableFieldSuggestionsTest do | ||
use AbsintheSecurityTest.AbsinthePhaseCase, | ||
phase: AbsintheSecurity.Phase.DisableFieldSuggestions, | ||
schema: __MODULE__.Schema, | ||
async: true | ||
|
||
defmodule Schema do | ||
use Absinthe.Schema | ||
|
||
query do | ||
field :foo_object, :foo do | ||
end | ||
end | ||
|
||
object :foo do | ||
field :bar, :string | ||
field :buzz, :integer | ||
end | ||
end | ||
|
||
describe "field suggestions" do | ||
test "are returned when it's enabled" do | ||
query = """ | ||
query FooObject { | ||
fooObject { | ||
buz | ||
} | ||
} | ||
""" | ||
|
||
assert {:ok, result, _} = run_phase(query, operation_name: "FooObject", variables: %{}, enable_field_suggestions: true, jump_phases: true) | ||
|
||
[error] = result.result.errors | ||
assert error.message === "Cannot query field \"buz\" on type \"Foo\". Did you mean \"buzz\"?" | ||
end | ||
|
||
test "are not returned when it's disabled" do | ||
query = """ | ||
query FooObject { | ||
fooObject { | ||
buz | ||
} | ||
} | ||
""" | ||
|
||
assert {:ok, result, _} = run_phase(query, operation_name: "FooObject", variables: %{}, enable_field_suggestions: false, jump_phases: true) | ||
|
||
[error] = result.result.errors | ||
assert error.message === "Cannot query field \"buz\" on type \"Foo\"." | ||
end | ||
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