Skip to content

Commit 7ebe854

Browse files
committed
GraphQLError: fix empty locations if error got nodes without locations
Replicates graphql/graphql-js@b936411
1 parent 2f135a3 commit 7ebe854

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/graphql/error/graphql_error.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,26 @@ def __init__(
137137
if nodes and not isinstance(nodes, list):
138138
nodes = [nodes] # type: ignore
139139
self.nodes = nodes or None # type: ignore
140+
node_locations = (
141+
[node.loc for node in nodes if node.loc] if nodes else [] # type: ignore
142+
)
140143

141144
# Compute locations in the source for the given nodes/positions.
142145
self.source = source
143-
if not source and nodes:
144-
node = nodes[0] # type: ignore
145-
if node and node.loc and node.loc.source:
146-
self.source = node.loc.source
147-
148-
if not positions and nodes:
149-
positions = [node.loc.start for node in nodes if node.loc] # type: ignore
146+
if not source and node_locations:
147+
loc = node_locations[0]
148+
if loc.source: # pragma: no cover else
149+
self.source = loc.source
150+
if not positions and node_locations:
151+
positions = [loc.start for loc in node_locations]
150152
self.positions = positions or None
151-
152153
if positions and source:
153154
locations: Optional[List["SourceLocation"]] = [
154155
source.get_location(pos) for pos in positions
155156
]
156-
elif nodes:
157-
locations = [
158-
node.loc.source.get_location(node.loc.start)
159-
for node in nodes # type: ignore
160-
if node.loc
161-
]
162157
else:
163-
locations = None
164-
self.locations = locations
158+
locations = [loc.source.get_location(loc.start) for loc in node_locations]
159+
self.locations = locations or None
165160

166161
if original_error:
167162
self.__traceback__ = original_error.__traceback__

tests/error/test_graphql_error.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ def converts_node_with_loc_start_zero_to_positions_and_locations():
116116
assert e.positions == [0]
117117
assert e.locations == [(1, 1)]
118118

119+
def converts_node_without_location_to_source_positions_and_locations_as_none():
120+
document_node = parse("{ foo }", no_location=True)
121+
122+
e = GraphQLError("msg", document_node)
123+
assert e.nodes == [document_node]
124+
assert e.source is None
125+
assert e.positions is None
126+
assert e.locations is None
127+
119128
def converts_source_and_positions_to_locations():
120129
e = GraphQLError("msg", None, source, [6])
121130
assert e.nodes is None

tests/validation/test_validation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ def _validate_document(max_errors=None):
158158
def _invalid_field_error(field_name: str):
159159
return {
160160
"message": f"Cannot query field '{field_name}' on type 'QueryRoot'.",
161-
"locations": [],
162161
}
163162

164163
def when_max_errors_is_equal_to_number_of_errors():

0 commit comments

Comments
 (0)