Skip to content

Commit

Permalink
Attempt to find the part after the minus
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Nov 29, 2024
1 parent f11fe5e commit 1c8ded0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
12 changes: 3 additions & 9 deletions lib/graphql/language/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def advance
# Check for a matched decimal:
@scanner[1] ? :FLOAT : :INT
else
# Attempt to find the part after the `-`
value = @scanner.scan(/-\s?[a-z0-9]*/i)
invalid_byte_for_number_error_message = "Expected type 'number', but it was malformed#{value.nil? ? "" : ": #{value.inspect}"}."
raise_parse_error(invalid_byte_for_number_error_message)
end
when ByteFor::ELLIPSIS
Expand Down Expand Up @@ -116,15 +119,6 @@ def debug_token_value(token_name)
end
end

def invalid_byte_for_number_error_message
match_data = @string.match(/\{\s*(\w+)\((\w+):\s*([^\)]+)\)\s*\}/)
field = match_data[1]
argument = match_data[2]
value = match_data[3]

"Argument '#{argument}' on Field '#{field}' has an invalid value (#{value}). Expected type 'number'."
end

ESCAPES = /\\["\\\/bfnrt]/
ESCAPES_REPLACE = {
'\\"' => '"',
Expand Down
49 changes: 26 additions & 23 deletions spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
expected_message = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [1, 8]"
else
"Argument 'b' on Field 'a' has an invalid value (-c). Expected type 'number'."
"Expected type 'number', but it was malformed: \"-c\"."
end
assert_equal expected_message, err.message
end
Expand All @@ -125,28 +125,24 @@
err = assert_raises GraphQL::ParseError do
GraphQL.parse("query($something: Int = -foo) { }")
end
# TODO: this currently raises a NoMethodError:
# graphql-ruby/lib/graphql/language/lexer.rb:121:in `invalid_byte_for_number_error_message'
# graphql-ruby/lib/graphql/language/lexer.rb:75:in `advance'
# graphql-ruby/lib/graphql/language/parser.rb:97:in `advance_token'
# graphql-ruby/lib/graphql/language/parser.rb:166:in `definition'
# graphql-ruby/lib/graphql/language/parser.rb:108:in `document'
# graphql-ruby/lib/graphql/language/parser.rb:45:in `block in parse'
# graphql-ruby/lib/graphql/tracing/trace.rb:24:in `parse'
# graphql-ruby/lib/graphql/language/parser.rb:44:in `parse'
# graphql-ruby/lib/graphql/language/parser.rb:16:in `parse'
# graphql-ruby/lib/graphql.rb:39:in `parse'
# graphql-ruby/spec/graphql/language/parser_spec.rb:126:in `block (3 levels) in <top (required)>'
assert_equal "TODO: add expected error messages here", err.message
expected_message = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [1, 25]"
else
"Expected type 'number', but it was malformed: \"-foo\"."
end
assert_equal expected_message, err.message
end

it "handles invalid minus signs in deeply nested input objects" do
err = assert_raises GraphQL::ParseError do
GraphQL.parse("{ doSomething(a: { b: { c: { d: -foo } } }) }")
end
# TODO: this currently removes `a:` from the error message:
# "Argument 'a' on Field 'doSomething' has an invalid value ({ b: { c: { d: -foo } } }). Expected type 'number'."
assert_equal "TODO: add expected error messages here", err.message
expected_message = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [1, 33]"
else
"Expected type 'number', but it was malformed: \"-foo\"."
end
assert_equal expected_message, err.message
end

it "handles invalid minus signs in schema definitions" do
Expand All @@ -157,9 +153,12 @@
}
")
end

# TODO: this currently raises the same NoMethodError as the other test
assert_equal "TODO: add expected error messages here", err.message
expected_message = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [3, 28]"
else
"Expected type 'number', but it was malformed: \"-foo\"."
end
assert_equal expected_message, err.message
end

it "handles invalid minus signs in list literals" do
Expand All @@ -169,8 +168,12 @@
a2: a(b: [1, 2, -foo])
}")
end
# TODO: this currently raises the same NoMethodError as the other test
assert_equal "TODO: add expected error messages here", err.message
expected_message = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [3, 25]"
else
"Expected type 'number', but it was malformed: \"-foo\"."
end
assert_equal expected_message, err.message
end

it "allows operation names to match operation types" do
Expand Down Expand Up @@ -244,7 +247,7 @@
expected_msg = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [1, 19]"
else
"Argument 'argument' on Field 'field' has an invalid value (a-b). Expected type 'number'."
"Expected type 'number', but it was malformed: \"-b\"."
end

assert_equal expected_msg, err.message
Expand Down

0 comments on commit 1c8ded0

Please sign in to comment.