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

Don't require input object argument when default value is configured #5133

Merged
merged 1 commit into from
Oct 24, 2024
Merged
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/schema/input_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def validate_non_null_input(input, ctx, max_errors: nil)

# Inject missing required arguments
missing_required_inputs = ctx.types.arguments(self).reduce({}) do |m, (argument)|
if !input.key?(argument.graphql_name) && argument.type.non_null? && types.argument(self, argument.graphql_name)
if !input.key?(argument.graphql_name) && argument.type.non_null? && !argument.default_value? && types.argument(self, argument.graphql_name)
m[argument.graphql_name] = nil
end

Expand Down
31 changes: 31 additions & 0 deletions spec/graphql/schema/argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,35 @@ def test; end
assert_equal expected_errors, schema.execute(query).to_h['errors']
end
end

describe "default values for non-null input object arguments when not present in variables" do
class InputObjectArgumentWithDefaultValueSchema < GraphQL::Schema
class Add < GraphQL::Schema::Resolver
class AddInput < GraphQL::Schema::InputObject
argument :a, Integer
argument :b, Integer
argument :c, Integer, default_value: 10
end

argument :input, AddInput
type(Integer, null: false)

def resolve(input:)
input[:a] + input[:b] + input[:c]
end
end
class Query < GraphQL::Schema::Object
field :add, resolver: Add
end
query(Query)
end

it "uses the default value" do
res1 = InputObjectArgumentWithDefaultValueSchema.execute("{ add(input: { a: 1, b: 2 })}")
assert_equal 13, res1["data"]["add"]

res2 = InputObjectArgumentWithDefaultValueSchema.execute("query Add($input: AddInput!) { add(input: $input) }", variables: { input: { a: 1, b: 4 } })
assert_equal 15, res2["data"]["add"]
end
end
end
Loading