Skip to content

Commit

Permalink
Allow directives on variable variable_definition_test (#1028)
Browse files Browse the repository at this point in the history
See graphql/graphql-spec#510

Adds support for directives on variable definitions in the parser.
  • Loading branch information
maartenvanvliet authored Jan 11, 2021
1 parent 5e54c50 commit 2aed570
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/absinthe/blueprint/document/variable_definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Absinthe.Blueprint.Document.VariableDefinition do
defstruct [
:name,
:type,
directives: [],
default_value: nil,
source_location: nil,
# Added by phases
Expand All @@ -19,6 +20,7 @@ defmodule Absinthe.Blueprint.Document.VariableDefinition do
@type t :: %__MODULE__{
name: String.t(),
type: Blueprint.TypeReference.t(),
directives: [Blueprint.Directive.t()],
default_value: Blueprint.Input.t(),
source_location: nil | Blueprint.SourceLocation.t(),
provided_value: nil | Blueprint.Input.t(),
Expand Down
2 changes: 1 addition & 1 deletion lib/absinthe/blueprint/transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule Absinthe.Blueprint.Transform do
Blueprint.Document.Fragment.Inline => [:selections, :directives],
Blueprint.Document.Fragment.Named => [:selections, :directives],
Blueprint.Document.Fragment.Spread => [:directives],
Blueprint.Document.VariableDefinition => [:type, :default_value],
Blueprint.Document.VariableDefinition => [:type, :default_value, :directives],
Blueprint.Input.Argument => [:input_value],
Blueprint.Input.Field => [:input_value],
Blueprint.Input.Object => [:fields],
Expand Down
2 changes: 2 additions & 0 deletions lib/absinthe/language/variable_definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Absinthe.Language.VariableDefinition do

defstruct variable: nil,
type: nil,
directives: [],
default_value: nil,
loc: %{line: nil}

Expand All @@ -20,6 +21,7 @@ defmodule Absinthe.Language.VariableDefinition do
%Blueprint.Document.VariableDefinition{
name: node.variable.name,
type: Blueprint.Draft.convert(node.type, doc),
directives: Absinthe.Blueprint.Draft.convert(node.directives, doc),
default_value: Blueprint.Draft.convert(node.default_value, doc),
source_location: source_location(node)
}
Expand Down
1 change: 1 addition & 0 deletions src/absinthe_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ VariableDefinitionList -> VariableDefinition : ['$1'].
VariableDefinitionList -> VariableDefinition VariableDefinitionList : ['$1'|'$2'].
VariableDefinition -> Variable ':' Type : build_ast_node('VariableDefinition', #{'variable' => '$1', 'type' => '$3'}, extract_child_location('$1')).
VariableDefinition -> Variable ':' Type DefaultValue : build_ast_node('VariableDefinition', #{'variable' => '$1', 'type' => '$3', 'default_value' => '$4'}, extract_child_location('$1')).
VariableDefinition -> Variable ':' Type DefaultValue Directives : build_ast_node('VariableDefinition', #{'variable' => '$1', 'type' => '$3', 'default_value' => '$4', 'directives' => '$5'}, extract_child_location('$1')).
Variable -> '$' NameWithoutOn : build_ast_node('Variable', #{'name' => extract_binary('$2')}, extract_location('$1')).
Variable -> '$' 'on' : build_ast_node('Variable', #{'name' => extract_binary('$2')}, extract_location('$1')).

Expand Down
3 changes: 2 additions & 1 deletion test/absinthe/language/variable_definition_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Absinthe.Language.VariableDefinitionTest do
alias Absinthe.{Blueprint, Language}

@query """
query Foo($showFoo: Boolean = true) {
query Foo($showFoo: Boolean = true @bar(a: 1)) {
foo @include(if: $showFoo)
}
"""
Expand All @@ -13,6 +13,7 @@ defmodule Absinthe.Language.VariableDefinitionTest do
test "builds a VariableDefinition.t" do
assert %Blueprint.Document.VariableDefinition{
name: "showFoo",
directives: [%Blueprint.Directive{name: "bar"}],
type: %Blueprint.TypeReference.Name{name: "Boolean"},
default_value: %Blueprint.Input.Boolean{value: true},
source_location: %Blueprint.SourceLocation{line: 1}
Expand Down

0 comments on commit 2aed570

Please sign in to comment.