Skip to content

Commit 4a94ae7

Browse files
committed
GraphQLError: fix empty locations if error got nodes without locations
Replicates graphql/graphql-js@8ba5c56
1 parent 5593c91 commit 4a94ae7

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/graphql/error/graphql_error.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any, Collection, Dict, List, Optional, Union, TYPE_CHECKING
33

44
if TYPE_CHECKING:
5-
from ..language.ast import Node # noqa: F401
5+
from ..language.ast import Node, Location # noqa: F401
66
from ..language.location import SourceLocation # noqa: F401
77
from ..language.source import Source # noqa: F401
88

@@ -95,27 +95,24 @@ def __init__(
9595
if nodes and not isinstance(nodes, list):
9696
nodes = [nodes] # type: ignore
9797
self.nodes = nodes or None # type: ignore
98+
node_locations = (
99+
[node.loc for node in nodes if node.loc] if nodes else [] # type: ignore
100+
)
98101
self.source = source
99-
if not source and nodes:
100-
node = nodes[0] # type: ignore
101-
if node and node.loc and node.loc.source:
102-
self.source = node.loc.source
103-
if not positions and nodes:
104-
positions = [node.loc.start for node in nodes if node.loc] # type: ignore
102+
if not source and node_locations:
103+
loc = node_locations[0]
104+
if loc.source: # pragma: no cover else
105+
self.source = loc.source
106+
if not positions and node_locations:
107+
positions = [loc.start for loc in node_locations]
105108
self.positions = positions or None
106109
if positions and source:
107110
locations: Optional[List["SourceLocation"]] = [
108111
source.get_location(pos) for pos in positions
109112
]
110-
elif nodes:
111-
locations = [
112-
node.loc.source.get_location(node.loc.start)
113-
for node in nodes # type: ignore
114-
if node.loc
115-
]
116113
else:
117-
locations = None
118-
self.locations = locations
114+
locations = [loc.source.get_location(loc.start) for loc in node_locations]
115+
self.locations = locations or None
119116
if path and not isinstance(path, list):
120117
path = list(path)
121118
self.path = path or None # type: ignore

tests/error/test_graphql_error.py

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

113+
def converts_node_without_location_to_source_positions_and_locations_as_none():
114+
document_node = parse("{ foo }", no_location=True)
115+
116+
e = GraphQLError("msg", document_node)
117+
assert e.nodes == [document_node]
118+
assert e.source is None
119+
assert e.positions is None
120+
assert e.locations is None
121+
113122
def converts_source_and_positions_to_locations():
114123
e = GraphQLError("msg", None, source, [6])
115124
assert e.nodes is None

tests/validation/test_validation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ def _validate_document(max_errors=None):
155155
def _invalid_field_error(field_name: str):
156156
return {
157157
"message": f"Cannot query field '{field_name}' on type 'QueryRoot'.",
158-
"locations": [],
159158
}
160159

161160
def when_max_errors_is_equal_to_number_of_errors():

0 commit comments

Comments
 (0)