Skip to content

Commit

Permalink
Merge pull request #4987 from rmosolgo/enum-value-keywords
Browse files Browse the repository at this point in the history
Fix enum values that match keywords
  • Loading branch information
rmosolgo authored Jun 12, 2024
2 parents 4e0ad5a + eda220b commit 43e377b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
3 changes: 0 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ Rake::TestTask.new do |t|

# After 2.7, there were not warnings for uninitialized ivars anymore
if RUBY_VERSION < "3"
puts "Disabling warnings on Ruby #{RUBY_VERSION.inspect}"
t.warning = false
else
puts "Enabling warnings on Ruby #{RUBY_VERSION.inspect}"
end
end

Expand Down
64 changes: 54 additions & 10 deletions lib/graphql/language/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,12 @@ def parse_enum_value_definitions
v_loc = pos
description = if at?(:STRING); string_value; end
defn_loc = pos
enum_value = parse_enum_name
# Any identifier, but not true, false, or null
enum_value = if at?(:TRUE) || at?(:FALSE) || at?(:NULL)
expect_token(:IDENTIFIER)
else
parse_name
end
v_directives = parse_directives
list << EnumValueDefinition.new(pos: v_loc, definition_pos: defn_loc, description: description, name: enum_value, directives: v_directives, filename: @filename, source: self)
end
Expand Down Expand Up @@ -631,15 +636,6 @@ def parse_name_without_on
end
end

# Any identifier, but not true, false, or null
def parse_enum_name
if at?(:TRUE) || at?(:FALSE) || at?(:NULL)
expect_token(:IDENTIFIER)
else
parse_name
end
end

def parse_type_name
TypeName.new(pos: pos, name: parse_name, filename: @filename, source: self)
end
Expand Down Expand Up @@ -730,6 +726,54 @@ def value
loc = pos
advance_token
VariableIdentifier.new(pos: loc, name: parse_name, filename: @filename, source: self)
when :SCHEMA
advance_token
Nodes::Enum.new(pos: pos, name: "schema", filename: @filename, source: self)
when :SCALAR
advance_token
Nodes::Enum.new(pos: pos, name: "scalar", filename: @filename, source: self)
when :IMPLEMENTS
advance_token
Nodes::Enum.new(pos: pos, name: "implements", filename: @filename, source: self)
when :INTERFACE
advance_token
Nodes::Enum.new(pos: pos, name: "interface", filename: @filename, source: self)
when :UNION
advance_token
Nodes::Enum.new(pos: pos, name: "union", filename: @filename, source: self)
when :ENUM
advance_token
Nodes::Enum.new(pos: pos, name: "enum", filename: @filename, source: self)
when :INPUT
advance_token
Nodes::Enum.new(pos: pos, name: "input", filename: @filename, source: self)
when :DIRECTIVE
advance_token
Nodes::Enum.new(pos: pos, name: "directive", filename: @filename, source: self)
when :TYPE
advance_token
Nodes::Enum.new(pos: pos, name: "type", filename: @filename, source: self)
when :QUERY
advance_token
Nodes::Enum.new(pos: pos, name: "query", filename: @filename, source: self)
when :MUTATION
advance_token
Nodes::Enum.new(pos: pos, name: "mutation", filename: @filename, source: self)
when :SUBSCRIPTION
advance_token
Nodes::Enum.new(pos: pos, name: "subscription", filename: @filename, source: self)
when :FRAGMENT
advance_token
Nodes::Enum.new(pos: pos, name: "fragment", filename: @filename, source: self)
when :REPEATABLE
advance_token
Nodes::Enum.new(pos: pos, name: "repeatable", filename: @filename, source: self)
when :ON
advance_token
Nodes::Enum.new(pos: pos, name: "on", filename: @filename, source: self)
when :EXTEND
advance_token
Nodes::Enum.new(pos: pos, name: "extend", filename: @filename, source: self)
else
expect_token(:VALUE)
end
Expand Down
9 changes: 8 additions & 1 deletion spec/graphql/dataloader/source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ def fetch(keys)
dl.with(FailsToLoadSource).request(nil)

source_cache = dl.instance_variable_get(:@source_cache)
assert source_cache[FailsToLoadSource][[{}]].pending?
source_cache_for_source = source_cache[FailsToLoadSource]

# The value of this changed in Ruby 3.3.3, see https://bugs.ruby-lang.org/issues/20180
# In previous versions, it was `[{}]`, but now it's `[]`
empty_batch_key = [*[], **{}]
source_inst = source_cache_for_source[empty_batch_key]
assert_instance_of FailsToLoadSource, source_inst, "The cache includes a pending source (#{source_cache_for_source.inspect})"
assert source_inst.pending?
end

class CustomKeySource < GraphQL::Dataloader::Source
Expand Down
14 changes: 10 additions & 4 deletions spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@
assert GraphQL.parse("{ field(null: false) ... null } fragment null on Query { null }")
end

it "allows fields and arguments named on and directive" do
assert GraphQL.parse("{ on(on: false) directive(directive: false)}")
it "allows fields, arguments, and enum values named on and directive" do
assert GraphQL.parse("{ on(on: on) directive(directive: directive)}")
end

it "allows fields and arguments extend" do
assert GraphQL.parse("{ extend(extend: false) }")
it "allows fields, arguments, and enum values named extend" do
assert GraphQL.parse("{ extend(extend: extend) }")
end

it "allows fields, arguments, and enum values named type" do
doc = GraphQL.parse("{ type(type: type) }")
assert_instance_of GraphQL::Language::Nodes::Enum, doc.definitions.first.selections.first.arguments.first.value

end

it "raises an error when unicode is used as names" do
Expand Down

0 comments on commit 43e377b

Please sign in to comment.