Skip to content

Commit

Permalink
Merge pull request #487 from martijnwalraven/graphqlerror-missing-loc…
Browse files Browse the repository at this point in the history
…ations

Fix GraphQLError missing positions/locations when node.loc.start === 0
  • Loading branch information
leebyron authored Sep 29, 2016
2 parents 948864e + 8a9addc commit a0c25e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/error/GraphQLError.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export function GraphQLError( // eslint-disable-line no-redeclare

let _positions = positions;
if (!_positions && nodes) {
_positions = nodes.map(node => node.loc && node.loc.start).filter(Boolean);
_positions = nodes.filter(node => node.loc !== null)
.map(node => node.loc.start);
}
if (_positions && _positions.length === 0) {
_positions = undefined;
Expand Down
13 changes: 13 additions & 0 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ describe('GraphQLError', () => {
expect(e.locations).to.deep.equal([ { line: 2, column: 7 } ]);
});

it('converts node with loc.start === 0 to positions and locations', () => {
const source = new Source(`{
field
}`);
const ast = parse(source);
const operationAST = ast.definitions[0];
const e = new GraphQLError('msg', [ operationAST ]);
expect(e.nodes).to.deep.equal([ operationAST ]);
expect(e.source).to.equal(source);
expect(e.positions).to.deep.equal([ 0 ]);
expect(e.locations).to.deep.equal([ { line: 1, column: 1 } ]);
});

it('converts source and positions to locations', () => {
const source = new Source(`{
field
Expand Down

0 comments on commit a0c25e5

Please sign in to comment.