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

BugFix: Nested List Input Coercion not matching GraphQL spec #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/graphql/utilities/coerce_input_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,19 @@ def coerce_input_value(
if is_iterable(input_value):
coerced_list: List[Any] = []
append_item = coerced_list.append
is_nested_list = bool(
is_list_type(item_type) and len(tuple(input_value)) > 1
)
for index, item_value in enumerate(input_value):
if is_nested_list and not is_iterable(item_value):
# Input values should be iterable for multivalued nested list type
on_error(
path.as_list() if path else Path(path, index, None).as_list(),
item_value,
GraphQLError(
f"Expected type '{inspect(item_type)}' to be a list."
),
)
append_item(
coerce_input_value(
item_value, item_type, on_error, Path(path, index, None)
Expand Down
17 changes: 14 additions & 3 deletions tests/utilities/test_coerce_input_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,24 @@ def returns_null_for_a_null_value():
result = _coerce_value(None, TestNestedList)
assert expect_value(result) is None

def returns_nested_list_for_nested_non_list_values():
def returns_error_for_nested_non_list_values():
result = _coerce_value([1, 2, 3], TestNestedList)
assert expect_value(result) == [[1], [2], [3]]
assert expect_errors(result) == [
("Expected type '[Int]' to be a list.", [0], 1),
("Expected type '[Int]' to be a list.", [1], 2),
("Expected type '[Int]' to be a list.", [2], 3),
]

def returns_nested_null_for_nested_null_values():
result = _coerce_value([[None], [None]], TestNestedList)
assert expect_value(result) == [[None], [None]]

def returns_errors_for_null_values():
result = _coerce_value([42, [None], None], TestNestedList)
assert expect_value(result) == [[42], [None], None]
assert expect_errors(result) == [
("Expected type '[Int]' to be a list.", [0], 42),
("Expected type '[Int]' to be a list.", [2], None),
]

def describe_with_default_on_error():
def throw_error_without_path():
Expand Down