Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQLError: fix empty locations if error got nodes without locations #3332

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/error/GraphQLError.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { ASTNode } from '../language/ast';
import { Source } from '../language/source';
import { SourceLocation } from '../language/location';

/**
* Custom extensions
*
* @remarks
* Use a unique identifier name for your extension, for example the name of
* your library or project. Do not use a shortened identifier as this increases
* the risk of conflicts. We recommend you add at most one extension field,
* an object which can contain all the values you need.
*/
export interface GraphQLErrorExtensions {
[attributeName: string]: any;
}

/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
Expand All @@ -18,7 +31,7 @@ export class GraphQLError extends Error {
positions?: Maybe<ReadonlyArray<number>>,
path?: Maybe<ReadonlyArray<string | number>>,
originalError?: Maybe<Error>,
extensions?: Maybe<{ [key: string]: any }>,
extensions?: Maybe<{ [key: string]: GraphQLErrorExtensions }>,
);

/**
Expand Down
68 changes: 28 additions & 40 deletions src/error/GraphQLError.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,33 @@ export class GraphQLError extends Error {
super(message);

// Compute list of blame nodes.
const _nodes = Array.isArray(nodes)
? nodes.length !== 0
? nodes
: undefined
: nodes
? [nodes]
: undefined;
const _nodes = undefinedIfEmpty(
Array.isArray(nodes) ? nodes : nodes ? [nodes] : undefined,
);

let nodeLocations = [];
for (const { loc } of _nodes ?? []) {
if (loc != null) {
nodeLocations.push(loc);
}
}
nodeLocations = undefinedIfEmpty(nodeLocations);

// Compute locations in the source for the given nodes/positions.
let _source = source;
if (!_source && _nodes) {
_source = _nodes[0].loc?.source;
}
const _source = source ?? nodeLocations?.[0].source;

let _positions = positions;
if (!_positions && _nodes) {
_positions = _nodes.reduce((list, node) => {
if (node.loc) {
list.push(node.loc.start);
}
return list;
}, []);
}
if (_positions && _positions.length === 0) {
_positions = undefined;
}
const _positions = positions ?? nodeLocations?.map((loc) => loc.start);

let _locations;
if (positions && source) {
_locations = positions.map((pos) => getLocation(source, pos));
} else if (_nodes) {
_locations = _nodes.reduce((list, node) => {
if (node.loc) {
list.push(getLocation(node.loc.source, node.loc.start));
}
return list;
}, []);
}
const _locations =
positions && source
? positions.map((pos) => getLocation(source, pos))
: nodeLocations?.map((loc) => getLocation(loc.source, loc.start));

let _extensions = extensions;
if (_extensions == null && originalError != null) {
const originalExtensions = originalError.extensions;
if (isObjectLike(originalExtensions)) {
_extensions = originalExtensions;
}
let _extensions = extensions ?? undefined;

const originalExtensions = originalError?.extensions;
if (isObjectLike(originalExtensions)) {
_extensions = originalExtensions;
}

Object.defineProperties((this: any), {
Expand Down Expand Up @@ -218,6 +200,12 @@ export class GraphQLError extends Error {
}
}

function undefinedIfEmpty<T>(
array: $ReadOnlyArray<T> | void,
): $ReadOnlyArray<T> | void {
return array === undefined || array.length === 0 ? undefined : array;
}

/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
Expand Down
12 changes: 12 additions & 0 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ describe('GraphQLError', () => {
});
});

it('converts node without location to undefined source, positions and locations', () => {
const documentNode = parse('{ foo }', { noLocation: true });

const e = new GraphQLError('msg', documentNode);
expect(e).to.deep.include({
nodes: [documentNode],
source: undefined,
positions: undefined,
locations: undefined,
});
});

it('converts source and positions to locations', () => {
const e = new GraphQLError('msg', null, source, [6]);
expect(e).to.have.property('source', source);
Expand Down
6 changes: 5 additions & 1 deletion src/error/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { GraphQLError, printError } from './GraphQLError';
export {
GraphQLError,
GraphQLErrorExtensions,
printError,
} from './GraphQLError';
export { syntaxError } from './syntaxError';
export { locatedError } from './locatedError';
export { formatError, GraphQLFormattedError } from './formatError';
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ export {
printError,
formatError,
GraphQLFormattedError,
GraphQLErrorExtensions,
} from './error/index';

// Utilities for operating on GraphQL type schema and parsed sources.
Expand Down
1 change: 0 additions & 1 deletion src/validation/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe('Validate: Limit maximum number of validation errors', () => {
function invalidFieldError(fieldName: string) {
return {
message: `Cannot query field "${fieldName}" on type "QueryRoot".`,
locations: [],
};
}

Expand Down