Skip to content

Commit 8ba5c56

Browse files
GraphQLError: fix empty locations if error got nodes without locations (#3332)
1 parent bbb1af5 commit 8ba5c56

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

src/error/GraphQLError.js

+28-40
Original file line numberDiff line numberDiff line change
@@ -87,51 +87,33 @@ export class GraphQLError extends Error {
8787
super(message);
8888

8989
// Compute list of blame nodes.
90-
const _nodes = Array.isArray(nodes)
91-
? nodes.length !== 0
92-
? nodes
93-
: undefined
94-
: nodes
95-
? [nodes]
96-
: undefined;
90+
const _nodes = undefinedIfEmpty(
91+
Array.isArray(nodes) ? nodes : nodes ? [nodes] : undefined,
92+
);
93+
94+
let nodeLocations = [];
95+
for (const { loc } of _nodes ?? []) {
96+
if (loc != null) {
97+
nodeLocations.push(loc);
98+
}
99+
}
100+
nodeLocations = undefinedIfEmpty(nodeLocations);
97101

98102
// Compute locations in the source for the given nodes/positions.
99-
let _source = source;
100-
if (!_source && _nodes) {
101-
_source = _nodes[0].loc?.source;
102-
}
103+
const _source = source ?? nodeLocations?.[0].source;
103104

104-
let _positions = positions;
105-
if (!_positions && _nodes) {
106-
_positions = _nodes.reduce((list, node) => {
107-
if (node.loc) {
108-
list.push(node.loc.start);
109-
}
110-
return list;
111-
}, []);
112-
}
113-
if (_positions && _positions.length === 0) {
114-
_positions = undefined;
115-
}
105+
const _positions = positions ?? nodeLocations?.map((loc) => loc.start);
116106

117-
let _locations;
118-
if (positions && source) {
119-
_locations = positions.map((pos) => getLocation(source, pos));
120-
} else if (_nodes) {
121-
_locations = _nodes.reduce((list, node) => {
122-
if (node.loc) {
123-
list.push(getLocation(node.loc.source, node.loc.start));
124-
}
125-
return list;
126-
}, []);
127-
}
107+
const _locations =
108+
positions && source
109+
? positions.map((pos) => getLocation(source, pos))
110+
: nodeLocations?.map((loc) => getLocation(loc.source, loc.start));
128111

129-
let _extensions = extensions;
130-
if (_extensions == null && originalError != null) {
131-
const originalExtensions = originalError.extensions;
132-
if (isObjectLike(originalExtensions)) {
133-
_extensions = originalExtensions;
134-
}
112+
let _extensions = extensions ?? undefined;
113+
114+
const originalExtensions = originalError?.extensions;
115+
if (isObjectLike(originalExtensions)) {
116+
_extensions = originalExtensions;
135117
}
136118

137119
Object.defineProperties((this: any), {
@@ -218,6 +200,12 @@ export class GraphQLError extends Error {
218200
}
219201
}
220202

203+
function undefinedIfEmpty<T>(
204+
array: $ReadOnlyArray<T> | void,
205+
): $ReadOnlyArray<T> | void {
206+
return array === undefined || array.length === 0 ? undefined : array;
207+
}
208+
221209
/**
222210
* Prints a GraphQLError to a string, representing useful location information
223211
* about the error's position in the source.

src/error/__tests__/GraphQLError-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ describe('GraphQLError', () => {
9696
});
9797
});
9898

99+
it('converts node without location to undefined source, positions and locations', () => {
100+
const documentNode = parse('{ foo }', { noLocation: true });
101+
102+
const e = new GraphQLError('msg', documentNode);
103+
expect(e).to.deep.include({
104+
nodes: [documentNode],
105+
source: undefined,
106+
positions: undefined,
107+
locations: undefined,
108+
});
109+
});
110+
99111
it('converts source and positions to locations', () => {
100112
const e = new GraphQLError('msg', null, source, [6]);
101113
expect(e).to.have.property('source', source);

src/validation/__tests__/validation-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ describe('Validate: Limit maximum number of validation errors', () => {
136136
function invalidFieldError(fieldName: string) {
137137
return {
138138
message: `Cannot query field "${fieldName}" on type "QueryRoot".`,
139-
locations: [],
140139
};
141140
}
142141

0 commit comments

Comments
 (0)