Skip to content

Commit a96c899

Browse files
authored
Merge pull request #5606 from rmosolgo/exec-next-more-input-fixes
Exec-next: more input fixes
2 parents 7267dd1 + e383faf commit a96c899

4 files changed

Lines changed: 72 additions & 45 deletions

File tree

lib/graphql/execution/input_values.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ def argument_values(owner_defn, argument_nodes, field_resolve_step)
4545
arg_node = argument_nodes.find { |a| a.name == arg_graphql_key }
4646
if arg_node.nil? || (arg_node.value.is_a?(Language::Nodes::VariableIdentifier) && !variable_values.key?(arg_node.value.name))
4747
if argument_definition.default_value?
48-
argument_value(argument_values, arg_ruby_key, argument_definition, argument_definition.default_value, nil, field_resolve_step)
48+
arg_value = value_from_ast(argument_definition.default_value, argument_definition.type)
49+
argument_value(argument_values, arg_ruby_key, argument_definition, arg_value, nil, field_resolve_step)
4950
end
5051
else
51-
argument_value(argument_values, arg_ruby_key, argument_definition, arg_node.value, nil, field_resolve_step)
52+
arg_value = value_from_ast(arg_node.value, argument_definition.type)
53+
argument_value(argument_values, arg_ruby_key, argument_definition, arg_value, nil, field_resolve_step)
5254
end
5355
end
5456

@@ -110,8 +112,6 @@ def argument_value(argument_values, argument_key, argument_definition, arg_value
110112
treat_as_type = treat_as_type.of_type
111113
end
112114

113-
arg_value = value_from_ast(arg_value, treat_as_type)
114-
115115
if treat_as_type.kind.list? && !arg_value.nil?
116116
inner_t = treat_as_type.unwrap
117117
arg_value = if arg_value.is_a?(Array)
@@ -125,14 +125,6 @@ def argument_value(argument_values, argument_key, argument_definition, arg_value
125125
end
126126
end
127127

128-
if override_type.nil? # only on root arguments, not list elements
129-
arg_value = begin
130-
argument_definition.prepare_value(nil, arg_value, context: @query.context)
131-
rescue StandardError => err
132-
@runner.schema.handle_or_reraise(@query.context, err)
133-
end
134-
end
135-
136128
if arg_value && treat_as_type.kind.input_object?
137129
arg_defns = @query.types.arguments(treat_as_type)
138130
new_arg_value = {}
@@ -151,7 +143,15 @@ def argument_value(argument_values, argument_key, argument_definition, arg_value
151143
end
152144
end
153145
end
154-
arg_value = new_arg_value
146+
arg_value = treat_as_type.new(nil, ruby_kwargs: new_arg_value, context: @query.context, defaults_used: nil)
147+
end
148+
149+
if override_type.nil? # only on root arguments, not list elements
150+
arg_value = begin
151+
argument_definition.prepare_value(nil, arg_value, context: @query.context)
152+
rescue StandardError => err
153+
@runner.schema.handle_or_reraise(@query.context, err)
154+
end
155155
end
156156

157157
if field_resolve_step && arg_value && override_type.nil? && argument_definition.loads

lib/graphql/schema/argument.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,6 @@ def recursively_prepare_input_object(value, type, context)
426426
elsif value.is_a?(GraphQL::Schema::InputObject)
427427
value.validate_for(context)
428428
value.prepare
429-
elsif value.is_a?(Hash)
430-
type.validate_for(value, context)
431-
value
432429
else
433430
value
434431
end

lib/graphql/schema/input_object.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ def validate_for(context)
112112
nil
113113
end
114114

115-
# @api private
116-
def self.validate_for(ruby_style_hash, context)
117-
# Pass this object's class with `as` so that messages are rendered correctly from inherited validators
118-
Schema::Validator.validate!(validators, nil, context, ruby_style_hash, as: self)
119-
nil
120-
end
121115
class << self
122116
def authorized?(obj, value, ctx)
123117
# Authorize each argument (but this doesn't apply if `prepare` is implemented):

spec/graphql/execution/input_values_spec.rb

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,41 @@
22
require "spec_helper"
33

44
class ExecutionInputValuesTest < Minitest::Test
5-
TEST_SCHEMA = GraphQL::Schema.from_definition(%|
6-
enum TestStatus {
7-
ACTIVE
8-
INACTIVE
9-
}
5+
class TestSchema < GraphQL::Schema
6+
class TestStatus < GraphQL::Schema::Enum
7+
value :ACTIVE
8+
value :INACTIVE
9+
end
1010

11-
input TestInput {
12-
string: String
13-
float: Float
14-
int: Int
15-
enum: TestStatus
16-
}
11+
class TestInput < GraphQL::Schema::InputObject
12+
argument :string, String, required: false
13+
argument :float, Float, required: false
14+
argument :int, Int, required: false
15+
argument :enum, TestStatus, required: false
16+
end
1717

18-
type Mutation {
19-
testInput(input: TestInput): Boolean
20-
}
18+
class Mutation < GraphQL::Schema::Object
19+
field :test_input, Boolean do
20+
argument :input, TestInput, required: false
21+
end
2122

22-
type Query {
23-
ping: Boolean
24-
}
25-
|)
23+
field :test_list_input, Boolean do
24+
argument :input, [TestInput, null: true], required: false
25+
end
26+
end
2627

28+
mutation(Mutation)
29+
query(Mutation) # Just to have something
30+
end
2731

2832
class DummyRunner
2933
def add_step(s); end
30-
def schema; TEST_SCHEMA; end
34+
def schema; TestSchema; end
3135
end
3236

33-
def get_input_values(variables_string: nil, variables: nil)
34-
query_str = "query#{variables_string ? "(#{variables_string})" : ""} { __typename }"
35-
query = GraphQL::Query.new(TEST_SCHEMA, query_str, validate: false, variables: variables)
37+
def get_input_values(query_string: nil, variables_string: nil, variables: nil)
38+
query_string ||= "query#{variables_string ? "(#{variables_string})" : ""} { __typename }"
39+
query = GraphQL::Query.new(TestSchema, query_string, validate: false, variables: variables)
3640
GraphQL::Execution::InputValues.new(query, DummyRunner.new)
3741
end
3842

@@ -67,6 +71,38 @@ def test_it_produces_argument_values_for_simple_scalars
6771

6872
def test_it_produces_argument_values_for_input_objects
6973
input = get_input_values
70-
assert_equal({input: { string: "a", enum: "ACTIVE" } }, input.argument_values(TEST_SCHEMA.find("Mutation.testInput"), get_argument_nodes("input: { string: \"a\", enum: ACTIVE }"), nil))
74+
assert_equal_input( {input: { string: "a", enum: "ACTIVE" } }, input.argument_values(TestSchema.find("Mutation.testInput"), get_argument_nodes("input: { string: \"a\", enum: ACTIVE }"), nil))
75+
end
76+
77+
def assert_equal_input(expected_ruby_hash, graphql_input, path = [])
78+
case expected_ruby_hash
79+
when Array
80+
assert_instance_of Array, graphql_input, "Matches at `#{path.join(".")}`"
81+
expected_ruby_hash.each_with_index do |next_expected, idx|
82+
assert_equal_input(next_expected, graphql_input[idx], path + [idx])
83+
end
84+
when Hash
85+
if path.empty?
86+
assert_instance_of Hash, graphql_input, "Matches at `#{path.join(".")}`"
87+
else
88+
assert_kind_of GraphQL::Schema::InputObject, graphql_input, "Matches at `#{path.join(".")}`"
89+
graphql_input = graphql_input.to_h
90+
end
91+
expected_ruby_hash.each do |k, v|
92+
assert_equal_input(v, graphql_input[k], path + [k])
93+
end
94+
else
95+
assert_equal expected_ruby_hash, graphql_input, "Matches at `#{path.join(".")}`"
96+
end
97+
end
98+
99+
def test_it_works_with_arrays_of_input_objects
100+
input = get_input_values(variables_string: "$string: String = \"abc\", $string2: String, $input: TestInput!", variables: { string2: "xyz", input: { string: "nested" }})
101+
assert_equal_input({input: [{}]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: $s }"), nil))
102+
assert_equal_input({input: [{ string: "Str" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: \"Str\" }"), nil))
103+
assert_equal_input({input: [{ string: "abc" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: $string }"), nil))
104+
assert_equal_input({input: [{ string: "xyz" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: $string2 }"), nil))
105+
assert_equal_input({input: [{ string: "nested" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: $input"), nil))
106+
assert_equal_input({input: [{}, {string: "Str"}, {string: "abc"}, {string: "xyz"}, {string: "nested"}]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: [{string: $s}, {string: \"Str\"}, {string: $string }, { string: $string2 }, $input]"), nil))
71107
end
72108
end

0 commit comments

Comments
 (0)