Skip to content

Commit

Permalink
Merge pull request #4847 from rmosolgo/variable-definition-directives
Browse files Browse the repository at this point in the history
Support variable definition directives
  • Loading branch information
rmosolgo authored Feb 16, 2024
2 parents cb78c85 + 3e81518 commit 95767bb
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 420 deletions.
838 changes: 424 additions & 414 deletions graphql-c_parser/ext/graphql_c_parser_ext/parser.c

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions graphql-c_parser/ext/graphql_c_parser_ext/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ SETUP_NODE_CLASS_VARIABLE(SchemaExtension)
| variable_definitions_list variable_definition { rb_ary_push($$, $2); }

variable_definition:
VAR_SIGN name COLON type default_value_opt {
$$ = MAKE_AST_NODE(VariableDefinition, 5,
VAR_SIGN name COLON type default_value_opt directives_list_opt {
$$ = MAKE_AST_NODE(VariableDefinition, 6,
rb_ary_entry($1, 1),
rb_ary_entry($1, 2),
rb_ary_entry($2, 3),
$4,
$5
$5,
$6
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/language/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class NonNullType < WrapperType
# An operation-level query variable
class VariableDefinition < AbstractNode
scalar_methods :name, :type, :default_value
children_methods false
children_methods(directives: Directive)
# @!attribute default_value
# @return [String, Integer, Float, Boolean, Array, NullValue] A Ruby value to use if no other value is provided

Expand Down
12 changes: 11 additions & 1 deletion lib/graphql/language/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ def definition
value
end

defs << Nodes::VariableDefinition.new(pos: loc, name: var_name, type: var_type, default_value: default_value, filename: @filename, source_string: @graphql_str)
directives = parse_directives

defs << Nodes::VariableDefinition.new(
pos: loc,
name: var_name,
type: var_type,
default_value: default_value,
directives: directives,
filename: @filename,
source_string: @graphql_str
)
end
expect_token(:RPAREN)
defs
Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/language/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ def print_variable_definition(variable_definition)
print_string(" = ")
print_node(variable_definition.default_value)
end
variable_definition.directives.each do |dir|
print_string(" ")
print_directive(dir)
end
end

def print_variable_identifier(variable_identifier)
Expand Down
5 changes: 5 additions & 0 deletions spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
end
end

it "parses directives on variable definitions" do
ast = GraphQL.parse("query($var: Int = 1 @special) { do(something: $var) }")
assert_equal ["special"], ast.definitions.first.variables.first.directives.map(&:name)
end

it "allows fragments, fields and arguments named null" do
assert GraphQL.parse("{ field(null: false) ... null } fragment null on Query { null }")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/language/printer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe GraphQL::Language::Printer do
let(:document) { GraphQL.parse(query_string) }
let(:query_string) {%|
query getStuff($someVar: Int = 1, $anotherVar: [String!], $skipNested: Boolean! = false) @skip(if: false) {
query getStuff($someVar: Int = 1, $anotherVar: [String!] @special(very: true), $skipNested: Boolean! = false) @skip(if: false) {
myField: someField(someArg: $someVar, ok: 1.4) @skip(if: $anotherVar) @thing(or: "Whatever")
anotherField(someArg: [1, 2, 3]) {
nestedField
Expand Down

0 comments on commit 95767bb

Please sign in to comment.