Skip to content

Commit e23decc

Browse files
committed
WIP
1 parent 1673f69 commit e23decc

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/graphql/schema/argument.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,28 @@ def coerce_into_values(parent_object, values, context, argument_values)
299299

300300
private
301301

302-
def validate_input_type(input_type)
302+
def validate_input_type(input_type, wrapped: false)
303303
if input_type.is_a?(String) || input_type.is_a?(GraphQL::Schema::LateBoundType)
304304
# Do nothing; assume this will be validated later
305+
false
305306
elsif input_type.kind.non_null? || input_type.kind.list?
306-
validate_input_type(input_type.unwrap)
307+
type_ready = validate_input_type(input_type.unwrap, wrapped: true)
308+
validate_default_value(default_value, input_type) if default_value? && type_ready && !wrapped
309+
type_ready
307310
elsif !input_type.kind.input?
308311
raise ArgumentError, "Invalid input type for #{path}: #{input_type.graphql_name}. Must be scalar, enum, or input object, not #{input_type.kind.name}."
309312
else
310-
# It's an input type, we're OK
313+
# It's an input type, the type itself is OK but make sure the default conforms
314+
validate_default_value(default_value, input_type) if default_value? && !wrapped
315+
true
311316
end
312317
end
313318

319+
def validate_default_value(default_value, input_type)
320+
default_value = input_type.coerce_isolated_result(default_value) unless default_value.nil?
321+
raise "BOOM" unless input_type.valid_isolated_input?(default_value)
322+
end
323+
314324
def validate_deprecated_or_optional(null:, deprecation_reason:)
315325
if deprecation_reason && !null
316326
raise ArgumentError, "Required arguments cannot be deprecated: #{path}."

spec/graphql/schema/argument_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ def self.object_from_id(id, ctx)
209209
res = SchemaArgumentTest::Schema.execute(query_str)
210210
assert_equal "Argument 'requiredWithDefaultArg' on Field 'field' has an invalid value (null). Expected type 'Int!'.", res['errors'][0]['message']
211211
end
212+
213+
it 'validates the type of the default value' do
214+
arg = GraphQL::Schema::Argument.new("my_arg", GraphQL::Types::Int, required: true, owner: nil, default_value: "a_string")
215+
assert_raises(StandardError) do
216+
arg.type
217+
end
218+
end
212219
end
213220

214221
describe 'loads' do

0 commit comments

Comments
 (0)