diff --git a/lib/graphql/introspection/input_value_type.rb b/lib/graphql/introspection/input_value_type.rb index 33e544ffe8..8a07b9932e 100644 --- a/lib/graphql/introspection/input_value_type.rb +++ b/lib/graphql/introspection/input_value_type.rb @@ -19,7 +19,7 @@ def is_deprecated def default_value if @object.default_value? - value = @object.default_value + value = @object.default_value(@context) if value.nil? 'null' else diff --git a/lib/graphql/schema/argument.rb b/lib/graphql/schema/argument.rb index 07098d5fd3..036ce2396f 100644 --- a/lib/graphql/schema/argument.rb +++ b/lib/graphql/schema/argument.rb @@ -87,7 +87,15 @@ def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil end # @return [Object] the value used when the client doesn't provide a value for this argument - attr_reader :default_value + def default_value(context = GraphQL::Query::NullContext) + return @default_value if @default_value == NO_DEFAULT + + coerced_value = @default_value.nil? ? nil : type.coerce_result(@default_value, context) + validation = type.validate_input(coerced_value, context) + raise validation.problems.first unless validation.valid? + + @default_value + end # @return [Boolean] True if this argument has a default value def default_value? @@ -250,7 +258,7 @@ def coerce_into_values(parent_object, values, context, argument_values) value = values[arg_key] elsif default_value? has_value = true - value = default_value + value = default_value(context) default_used = true end diff --git a/lib/graphql/schema/resolver.rb b/lib/graphql/schema/resolver.rb index a7aa8a25c6..ddbb2662ea 100644 --- a/lib/graphql/schema/resolver.rb +++ b/lib/graphql/schema/resolver.rb @@ -148,7 +148,7 @@ def ready?(**args) def authorized?(**inputs) self.class.arguments.each_value do |argument| arg_keyword = argument.keyword - if inputs.key?(arg_keyword) && !(arg_value = inputs[arg_keyword]).nil? && (arg_value != argument.default_value) + if inputs.key?(arg_keyword) && !(arg_value = inputs[arg_keyword]).nil? && (arg_value != argument.default_value(context)) arg_auth, err = argument.authorized?(self, arg_value, context) if !arg_auth return arg_auth, err diff --git a/spec/graphql/schema/argument_spec.rb b/spec/graphql/schema/argument_spec.rb index bfc823fff9..730d9989b3 100644 --- a/spec/graphql/schema/argument_spec.rb +++ b/spec/graphql/schema/argument_spec.rb @@ -211,6 +211,13 @@ def self.object_from_id(id, ctx) end 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: :some_symbol) + assert_raises(StandardError) do + arg.default_value + end + end + describe 'loads' do it "loads input object arguments" do query_str = <<-GRAPHQL