-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #999 from maartenvanvliet/add-repeatable-directives
Add support for repeatable directives
- Loading branch information
Showing
19 changed files
with
219 additions
and
13 deletions.
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
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
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 |
---|---|---|
|
@@ -302,6 +302,7 @@ defmodule Absinthe.Lexer do | |
on | ||
ON | ||
query | ||
repeatable | ||
scalar | ||
schema | ||
subscription | ||
|
86 changes: 86 additions & 0 deletions
86
lib/absinthe/phase/document/validation/repeatable_directives.ex
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,86 @@ | ||
defmodule Absinthe.Phase.Document.Validation.RepeatableDirectives do | ||
@moduledoc false | ||
|
||
alias Absinthe.{Blueprint, Phase} | ||
|
||
use Absinthe.Phase | ||
use Absinthe.Phase.Validation | ||
|
||
@doc """ | ||
Run the validation. | ||
""" | ||
@spec run(Blueprint.t(), Keyword.t()) :: Phase.result_t() | ||
def run(input, _options \\ []) do | ||
result = Blueprint.postwalk(input, &handle_node/1) | ||
{:ok, result} | ||
end | ||
|
||
defp handle_node(%Blueprint.Directive{} = node) do | ||
node | ||
end | ||
|
||
defp handle_node(%{directives: []} = node) do | ||
node | ||
end | ||
|
||
defp handle_node(%{directives: _} = node) do | ||
node | ||
|> check_directives | ||
|> inherit_invalid(node.directives, :bad_directive) | ||
end | ||
|
||
defp handle_node(node) do | ||
node | ||
end | ||
|
||
defp check_directives(node) do | ||
directives = | ||
for directive <- node.directives do | ||
case directive do | ||
%{schema_node: nil} -> | ||
directive | ||
|
||
%{schema_node: %{repeatable: true}} -> | ||
directive | ||
|
||
directive -> | ||
check_duplicates( | ||
directive, | ||
Enum.filter( | ||
node.directives, | ||
&compare_directive_schema_node(directive.schema_node, &1.schema_node) | ||
) | ||
) | ||
end | ||
end | ||
|
||
%{node | directives: directives} | ||
end | ||
|
||
defp compare_directive_schema_node(_, nil), do: false | ||
|
||
defp compare_directive_schema_node(%{identifier: identifier}, %{identifier: identifier}), | ||
do: true | ||
|
||
defp compare_directive_schema_node(_, _), do: false | ||
|
||
# Generate the error for the node | ||
@spec error_repeated(Blueprint.node_t()) :: Phase.Error.t() | ||
defp error_repeated(node) do | ||
%Phase.Error{ | ||
phase: __MODULE__, | ||
message: "Directive `#{node.name}' cannot be applied repeatedly.", | ||
locations: [node.source_location] | ||
} | ||
end | ||
|
||
defp check_duplicates(directive, [_single]) do | ||
directive | ||
end | ||
|
||
defp check_duplicates(directive, _multiple) do | ||
directive | ||
|> flag_invalid(:duplicate_directive) | ||
|> put_error(error_repeated(directive)) | ||
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
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
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
Oops, something went wrong.