Skip to content

Commit

Permalink
Fix highlighting for GraphQL errors with multiple nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Buryak committed Dec 16, 2022
1 parent 715e999 commit d281e03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,37 @@ describe('getDiagnostics', () => {
expect(error.source).toEqual('GraphQL: Validation');
});

it('validates undefined variables', () => {
it('catches with multiple highlighted nodes', () => {
const errors = validateQuery(
parse('{ hero(episode: $ep) { name } }'),
schema,
);
expect(errors).toMatchInlineSnapshot(`
Array [
Object {
"message": "Variable \\"$ep\\" is not defined.",
"range": Range {
"containsPosition": [Function],
"end": Position {
"character": 20,
"lessThanOrEqualTo": [Function],
"line": 0,
},
"start": Position {
"character": 16,
"lessThanOrEqualTo": [Function],
"line": 0,
},
expect(errors).toMatchObject([
{
range: {
end: {
character: 20,
line: 0,
},
start: {
character: 16,
line: 0,
},
"severity": 1,
"source": "GraphQL: Validation",
},
]
`);
},
{
range: {
end: {
character: 32,
line: 0,
},
start: {
character: 0,
line: 0,
},
},
},
]);
});

it('catches multi root validation errors without breaking (with a custom validation function that always throws errors)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function annotations(
return [];
}
const highlightedNodes: Diagnostic[] = [];
error.nodes.forEach(node => {
error.nodes.forEach((node, i) => {
const highlightNode =
node.kind !== 'Variable' && 'name' in node && node.name !== undefined
? node.name
Expand All @@ -154,7 +154,7 @@ function annotations(

// @ts-ignore
// https://github.com/microsoft/TypeScript/pull/32695
const loc = error.locations[0];
const loc = error.locations[i];
const highlightLoc = getLocation(highlightNode);
const end = loc.column + (highlightLoc.end - highlightLoc.start);
highlightedNodes.push({
Expand Down

0 comments on commit d281e03

Please sign in to comment.