Skip to content

Commit 58d058e

Browse files
committed
Fix nils in JSON Type hash values
1 parent fe87d07 commit 58d058e

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

lib/graphql/static_validation/literal_validator.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ def replace_nulls_in(ast_value)
2222
case ast_value
2323
when Array
2424
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
25+
when GraphQL::Language::Nodes::InputObject
26+
ast_value.to_h
3027
when GraphQL::Language::Nodes::NullValue
3128
nil
3229
else

spec/graphql/schema/build_from_definition_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,12 @@ def call(parent_type, field, object, args, context)
16781678
end
16791679

16801680
def coerce_input(type, value, ctx)
1681-
(ctx[:nils] ||= []).push(value[2])
1681+
nils = ctx[:nils] ||= []
1682+
if value.is_a?(Array)
1683+
nils << value[2]
1684+
else
1685+
nils << value["abc"]
1686+
end
16821687
::JSON.generate(value)
16831688
end
16841689

@@ -1707,6 +1712,23 @@ def coerce_result(type, value, ctx)
17071712
assert_equal([3, "abc", nil, 7], res_2["data"]["echoJsonValue"])
17081713
assert_equal [nil, nil], res_2.context[:nils]
17091714

1715+
res_3 = app.execute_query(<<~EOS, arg: { "abc" => nil, "def" => 7 })
1716+
query WithArg($arg: JsonValue) {
1717+
echoJsonValue(arg: $arg)
1718+
}
1719+
EOS
1720+
1721+
assert_equal({ "abc" => nil, "def" => 7 }, res_3["data"]["echoJsonValue"])
1722+
assert_equal [nil, nil], res_3.context[:nils]
1723+
1724+
res_4 = app.execute_query(<<~EOS)
1725+
query {
1726+
echoJsonValue(arg: { abc: null, def: 7, ghi: { jkl: null } })
1727+
}
1728+
EOS
1729+
1730+
assert_equal({ "abc" => nil, "def" => 7, "ghi"=>{"jkl"=>nil} }, res_4["data"]["echoJsonValue"])
1731+
assert_equal [nil, nil], res_4.context[:nils]
17101732
end
17111733
end
17121734
end

0 commit comments

Comments
 (0)