From 11a4c24a08cac9737aea5a46827a015bf9e81961 Mon Sep 17 00:00:00 2001 From: Evan Huus Date: Mon, 26 Apr 2021 17:00:22 +0000 Subject: [PATCH] WIP --- lib/graphql/schema/argument.rb | 16 +++++++++++++--- spec/graphql/schema/argument_spec.rb | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/graphql/schema/argument.rb b/lib/graphql/schema/argument.rb index 07098d5fd3..5698f5527a 100644 --- a/lib/graphql/schema/argument.rb +++ b/lib/graphql/schema/argument.rb @@ -301,18 +301,28 @@ def coerce_into_values(parent_object, values, context, argument_values) private - def validate_input_type(input_type) + def validate_input_type(input_type, wrapped: false) if input_type.is_a?(String) || input_type.is_a?(GraphQL::Schema::LateBoundType) # Do nothing; assume this will be validated later + false elsif input_type.kind.non_null? || input_type.kind.list? - validate_input_type(input_type.unwrap) + type_ready = validate_input_type(input_type.unwrap, wrapped: true) + validate_default_value(default_value, input_type) if default_value? && type_ready && !wrapped + type_ready elsif !input_type.kind.input? raise ArgumentError, "Invalid input type for #{path}: #{input_type.graphql_name}. Must be scalar, enum, or input object, not #{input_type.kind.name}." else - # It's an input type, we're OK + # It's an input type, the type itself is OK but make sure the default conforms + validate_default_value(default_value, input_type) if default_value? && !wrapped + true end end + def validate_default_value(default_value, input_type) + default_value = input_type.coerce_isolated_result(default_value) unless default_value.nil? + raise "BOOM" unless input_type.valid_isolated_input?(default_value) + end + def validate_deprecated_or_optional(null:, deprecation_reason:) if deprecation_reason && !null raise ArgumentError, "Required arguments cannot be deprecated: #{path}." diff --git a/spec/graphql/schema/argument_spec.rb b/spec/graphql/schema/argument_spec.rb index bfc823fff9..1960115a9d 100644 --- a/spec/graphql/schema/argument_spec.rb +++ b/spec/graphql/schema/argument_spec.rb @@ -209,6 +209,13 @@ def self.object_from_id(id, ctx) res = SchemaArgumentTest::Schema.execute(query_str) assert_equal "Argument 'requiredWithDefaultArg' on Field 'field' has an invalid value (null). Expected type 'Int!'.", res['errors'][0]['message'] end + + it 'validates the type of the default value' do + arg = GraphQL::Schema::Argument.new("my_arg", GraphQL::Types::Int, required: true, owner: nil, default_value: "a_string") + assert_raises(StandardError) do + arg.type + end + end end describe 'loads' do