Skip to content

Commit 5fc53f6

Browse files
committed
Correct static validation of nested InputObjects
1 parent 32918e8 commit 5fc53f6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/graphql/static_validation/rules/required_input_object_attributes_are_present.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@ def on_input_object(node, parent)
1212
private
1313

1414
def get_parent_type(context, parent)
15-
defn = context.field_definition
15+
# If argument_definition is defined we're at nested object
16+
# and need to refer to the containing input object type rather
17+
# than the field_definition.
18+
# h/t @rmosolgo
19+
arg_defn = context.argument_definition
20+
21+
# Double checking that arg_defn is an input object as nested
22+
# scalars, namely JSON, can make it to this branch
23+
defn = if arg_defn && arg_defn.type.unwrap.kind.input_object?
24+
arg_defn.type.unwrap
25+
else
26+
context.field_definition
27+
end
28+
1629
parent_type = context.warden.arguments(defn)
1730
.find{|f| f.name == parent_name(parent, defn) }
1831
parent_type ? parent_type.type.unwrap : nil

spec/graphql/static_validation/rules/required_input_object_attributes_are_present_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
yakSource: searchDairy(product: [{source: COW, fatContent: 1.1}]) { __typename }
1313
badSource: searchDairy(product: [{source: 1.1}]) { __typename }
1414
missingSource: searchDairy(product: [{fatContent: 1.1}]) { __typename }
15+
missingNestedRequiredInputObjectAttribute: searchDairy(product: [{fatContent: 1.2, order_by: {}}]) { __typename }
1516
listCoerce: cheese(id: 1) { similarCheese(source: YAK) { __typename } }
1617
missingInputField: searchDairy(product: [{source: YAK, wacky: 1}]) { __typename }
1718
}
@@ -42,16 +43,30 @@
4243
"inputObjectType"=>"DairyProductInput"
4344
}
4445
}
46+
missing_order_by_direction_error = {
47+
"message"=>"Argument 'direction' on InputObject 'ResourceOrderType' is required. Expected type String!",
48+
"locations"=>[{"line"=>8, "column"=>100}],
49+
"path"=>["query getCheese", "missingNestedRequiredInputObjectAttribute", "product", "order_by", "direction"],
50+
"extensions"=>{
51+
"code"=>"missingRequiredInputObjectAttribute",
52+
"argumentName"=>"direction",
53+
"argumentType"=>"String!",
54+
"inputObjectType"=>"ResourceOrderType"
55+
}
56+
}
4557
it "finds undefined or missing-required arguments to fields and directives" do
4658
without_error_bubbling(schema) do
4759
assert_includes(errors, missing_source_error)
60+
assert_includes(errors, missing_order_by_direction_error)
4861
refute_includes(errors, missing_required_field_error)
4962
end
5063
end
64+
# focus
5165
it 'works with error bubbling enabled' do
5266
with_error_bubbling(schema) do
5367
assert_includes(errors, missing_required_field_error)
5468
assert_includes(errors, missing_source_error)
69+
assert_includes(errors, missing_order_by_direction_error)
5570
end
5671
end
5772
end

0 commit comments

Comments
 (0)