Skip to content

Commit

Permalink
Also prepare lists-of-lists of input objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Apr 25, 2024
1 parent f03f448 commit 7ae33ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/graphql/schema/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,8 @@ def statically_coercible?
# Used by the runtime.
# @api private
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)
if type.unwrap.kind.input_object?
value = recursively_prepare_input_object(value, type)
end

Schema::Validator.validate!(validators, obj, context, value)
Expand Down Expand Up @@ -375,6 +373,21 @@ def initialize(argument)

private

def recursively_prepare_input_object(value, type)
if type.non_null?
type = type.of_type
end

if type.list?
inner_type = type.of_type
value.map { |v| recursively_prepare_input_object(v, inner_type) }
elsif value.is_a?(GraphQL::Schema::InputObject)
value.prepare
else
value
end
end

def validate_input_type(input_type)
if input_type.is_a?(String) || input_type.is_a?(GraphQL::Schema::LateBoundType)
# Do nothing; assume this will be validated later
Expand Down
11 changes: 11 additions & 0 deletions spec/graphql/schema/input_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ def prepare_list(input:)
def prepare_and_load(input:)
"#{input[:value]}/#{input[:thing][:name]}"
end

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

def prepare_list_of_lists(input:)
input.map { |i| i.map(&:prepared_count) }
end
end

class Schema < GraphQL::Schema
Expand Down Expand Up @@ -473,6 +481,9 @@ def self.object_from_id(id, _ctx)
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"]

res = InputObjectPrepareObjectTest::Schema.execute("{ prepareListOfLists( input:[[{ i: 1 }, { i: 1}], [{i: 2}, {i: 2}]]) }")
assert_equal [[1, 1], [1, 1]], res["data"]["prepareListOfLists"]
end

it "calls prepare on the input object (variable)" do
Expand Down

0 comments on commit 7ae33ae

Please sign in to comment.