Skip to content

Commit

Permalink
Merge pull request #5133 from rmosolgo/non-null-default-value-variabl…
Browse files Browse the repository at this point in the history
…e-fix

Don't require input object argument when default value is configured
  • Loading branch information
rmosolgo authored Oct 24, 2024
2 parents 44be301 + 8b2555e commit f318775
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit f318775

Please sign in to comment.