Skip to content

Commit 1e18183

Browse files
committed
fix: accept any collection of nodes in GraphQLError
The parser creates tuples of nodes, but only a list or a single node was handled, so passing such a tuple raised an AttributeError. Fixes #321
1 parent 9164df1 commit 1e18183

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/graphql/error/graphql_error.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ def __init__(
230230

231231
# Compute list of blame nodes.
232232
if nodes and not isinstance(nodes, list):
233-
nodes = [nodes] # type: ignore
233+
# The parser creates tuples of nodes, so any collection is accepted.
234+
nodes = list(nodes) if isinstance(nodes, Collection) else [nodes]
234235
self.nodes = nodes or None # type: ignore
235236
node_locations = (
236237
[node.loc for node in nodes if node.loc] if nodes else [] # type: ignore

‎tests/error/test_graphql_error.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import deque
12
from typing import cast, List, Union
23

34
from pytest import raises
@@ -162,6 +163,29 @@ def converts_single_node_to_positions_and_locations():
162163
assert e.positions == [4]
163164
assert e.locations == [(2, 3)]
164165

166+
def converts_tuple_of_nodes_to_positions_and_locations():
167+
operation = parse("{ first second }").definitions[0]
168+
nodes = cast(OperationDefinitionNode, operation).selection_set.selections
169+
assert isinstance(nodes, tuple)
170+
e = GraphQLError("msg", nodes)
171+
assert e.nodes == list(nodes)
172+
assert e.positions == [2, 8]
173+
assert e.locations == [(1, 3), (1, 9)]
174+
175+
def converts_other_collection_of_nodes_to_positions_and_locations():
176+
e = GraphQLError("msg", deque([field_node, operation_node]))
177+
assert e.nodes == [field_node, operation_node]
178+
assert e.source is source
179+
assert e.positions == [4, 0]
180+
assert e.locations == [(2, 3), (1, 1)]
181+
182+
def converts_empty_tuple_of_nodes_to_none():
183+
e = GraphQLError("msg", ())
184+
assert e.nodes is None
185+
assert e.source is None
186+
assert e.positions is None
187+
assert e.locations is None
188+
165189
def converts_node_with_loc_start_zero_to_positions_and_locations():
166190
e = GraphQLError("msg", operation_node)
167191
assert e.nodes == [operation_node]

0 commit comments

Comments
 (0)