Skip to content

Commit 8a9addc

Browse files
Fix GraphQLError missing positions/locations when node.loc.start === 0
Because the GraphQLError constructor filtered out falsy values for node.loc.start, that would also filter out 0.
1 parent 2406d3a commit 8a9addc

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/error/GraphQLError.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ export function GraphQLError( // eslint-disable-line no-redeclare
101101

102102
let _positions = positions;
103103
if (!_positions && nodes) {
104-
_positions = nodes.map(node => node.loc && node.loc.start).filter(Boolean);
104+
_positions = nodes.filter(node => node.loc !== null)
105+
.map(node => node.loc.start);
105106
}
106107
if (_positions && _positions.length === 0) {
107108
_positions = undefined;

src/error/__tests__/GraphQLError-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ describe('GraphQLError', () => {
7272
expect(e.locations).to.deep.equal([ { line: 2, column: 7 } ]);
7373
});
7474

75+
it('converts node with loc.start === 0 to positions and locations', () => {
76+
const source = new Source(`{
77+
field
78+
}`);
79+
const ast = parse(source);
80+
const operationAST = ast.definitions[0];
81+
const e = new GraphQLError('msg', [ operationAST ]);
82+
expect(e.nodes).to.deep.equal([ operationAST ]);
83+
expect(e.source).to.equal(source);
84+
expect(e.positions).to.deep.equal([ 0 ]);
85+
expect(e.locations).to.deep.equal([ { line: 1, column: 1 } ]);
86+
});
87+
7588
it('converts source and positions to locations', () => {
7689
const source = new Source(`{
7790
field

0 commit comments

Comments
 (0)