Skip to content

Commit e9e75da

Browse files
committed
Don't pass NullValue to literal validation
1 parent 4712a3d commit e9e75da

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

lib/graphql/static_validation/literal_validator.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ def validate(ast_value, type)
1818

1919
private
2020

21+
def replace_nulls_in(ast_value)
22+
case ast_value
23+
when Array
24+
ast_value.map { |v| replace_nulls_in(v) }
25+
when Hash
26+
new_v = {}
27+
ast_value.each do |k, v|
28+
new_v[k] = replace_nulls_in(v)
29+
end
30+
when GraphQL::Language::Nodes::NullValue
31+
nil
32+
else
33+
ast_value
34+
end
35+
end
36+
2137
def recursively_validate(ast_value, type)
2238
if type.nil?
2339
# this means we're an undefined argument, see #present_input_field_values_are_valid
@@ -42,7 +58,8 @@ def recursively_validate(ast_value, type)
4258
@valid_response
4359
elsif type.kind.scalar? && constant_scalar?(ast_value)
4460
maybe_raise_if_invalid(ast_value) do
45-
type.validate_input(ast_value, @context)
61+
ruby_value = replace_nulls_in(ast_value)
62+
type.validate_input(ruby_value, @context)
4663
end
4764
elsif type.kind.enum?
4865
maybe_raise_if_invalid(ast_value) do

spec/graphql/schema/build_from_definition_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,4 +1654,59 @@ class Link < GraphQL::Schema::Directive
16541654
GRAPHQL
16551655
assert_equal expected_schema, schema.to_definition
16561656
end
1657+
1658+
describe "JSON type" do
1659+
class JsonTypeApplication
1660+
SCHEMA_STRING = <<~EOS
1661+
scalar JsonValue
1662+
1663+
type Query {
1664+
echoJsonValue(arg: JsonValue): JsonValue
1665+
}
1666+
EOS
1667+
1668+
def initialize
1669+
@schema = GraphQL::Schema.from_definition(SCHEMA_STRING, default_resolve: self)
1670+
end
1671+
1672+
def execute_query(query_string, **vars)
1673+
@schema.execute(query_string, variables: vars)
1674+
end
1675+
1676+
def call(parent_type, field, object, args, context)
1677+
args.fetch(:arg)
1678+
end
1679+
1680+
def coerce_input(type, value, ctx)
1681+
(ctx[:nils] ||= []).push(value[2])
1682+
::JSON.generate(value)
1683+
end
1684+
1685+
def coerce_result(type, value, ctx)
1686+
::JSON.parse(value)
1687+
end
1688+
end
1689+
1690+
it "Sends normal ruby values to schema coercion" do
1691+
app = JsonTypeApplication.new
1692+
1693+
res_1 = app.execute_query(<<~EOS, arg: [3, "abc", nil, 7])
1694+
query WithArg($arg: JsonValue) {
1695+
echoJsonValue(arg: $arg)
1696+
}
1697+
EOS
1698+
1699+
assert_equal([3, "abc", nil, 7], res_1["data"]["echoJsonValue"])
1700+
assert_equal [nil, nil], res_1.context[:nils]
1701+
1702+
res_2 = app.execute_query(<<~EOS)
1703+
query {
1704+
echoJsonValue(arg: [3, "abc", null, 7])
1705+
}
1706+
EOS
1707+
assert_equal([3, "abc", nil, 7], res_2["data"]["echoJsonValue"])
1708+
assert_equal [nil, nil], res_2.context[:nils]
1709+
1710+
end
1711+
end
16571712
end

0 commit comments

Comments
 (0)