Skip to content

Commit

Permalink
Call prepare on lists of input objects
Browse files Browse the repository at this point in the history
When the argument is a list of input objects we need to call prepare on
each item of the list
  • Loading branch information
Heinrich Lee Yu committed Apr 25, 2024
1 parent 0799211 commit 52fca92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/graphql/schema/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def statically_coercible?
def prepare_value(obj, value, context: nil)
if value.is_a?(GraphQL::Schema::InputObject)
value = value.prepare
elsif type.list? && value[0].is_a?(GraphQL::Schema::InputObject)
value = value.map(&:prepare)
end

Schema::Validator.validate!(validators, obj, context, value)
Expand Down
13 changes: 13 additions & 0 deletions spec/graphql/schema/input_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ def inputs(input:)
def prepare_once(input:)
input.prepared_count
end

field :prepare_list, [Int] do
argument :input, [OnlyOnePrepareInputObject]
end

def prepare_list(input:)
input.map(&:prepared_count)
end
end

class Schema < GraphQL::Schema
Expand All @@ -428,6 +436,11 @@ class Schema < GraphQL::Schema
assert_equal 1, res["data"]["prepareOnce"]
end

it "calls prepare on lists of input objects" do
res = InputObjectPrepareObjectTest::Schema.execute("{ prepareList( input:[{ i: 1 }, { i: 1}]) }")
assert_equal [1, 1], res["data"]["prepareList"]
end

it "calls prepare on the input object (variable)" do
query_str = <<-GRAPHQL
query ($input: RangeInput!){ inputs(input: $input) }
Expand Down

0 comments on commit 52fca92

Please sign in to comment.