Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate argument defaults #3476

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql/introspection/input_value_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/graphql/schema/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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)
Copy link
Contributor Author

@eapache eapache May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmosolgo I'm looking at this a little more and I realize I'm confused. We call coerce_input on this value a few lines down, which at best is a no-op but (since this value should already be specified as ruby-land-objects and not graphql-syntax-input) would most likely fail entirely even on main branch without this PR? What am I missing?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm admittedly fuzzy on the details but the intent here is to prepare some GraphQL input for handling by the application. (Eventually, argument_values here will populate a GraphQL::Execution::Interpreter::Arguments instance.) So, unless I've misunderstood, I'd expect type.coerce_input(...) below to prepare a GraphQL value for handling by Ruby. (Eg, turning enum strings into their value: ...s.)

But maybe that's the wrong approach for default_values, if they're already Ruby-style!

default_used = true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/graphql/schema/argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down