Initial commit

This commit is contained in:
Luis Fernando Molina committed 2018-09-03 15:19:57 +02:00
commit f47e08fdcb
2409 files changed
+363607

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GraphQLError = GraphQLError;
var _printError = require("./printError");
var _location = require("../language/location");
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
function GraphQLError( // eslint-disable-line no-redeclare
message, nodes, source, positions, path, originalError, extensions) {
// Compute list of blame nodes.
var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions.
var _source = source;
if (!_source && _nodes) {
var node = _nodes[0];
_source = node && node.loc && node.loc.source;
}
var _positions = positions;
if (!_positions && _nodes) {
_positions = _nodes.reduce(function (list, node) {
if (node.loc) {
list.push(node.loc.start);
}
return list;
}, []);
}
if (_positions && _positions.length === 0) {
_positions = undefined;
}
var _locations;
if (positions && source) {
_locations = positions.map(function (pos) {
return (0, _location.getLocation)(source, pos);
});
} else if (_nodes) {
_locations = _nodes.reduce(function (list, node) {
if (node.loc) {
list.push((0, _location.getLocation)(node.loc.source, node.loc.start));
}
return list;
}, []);
}
var _extensions = extensions || originalError && originalError.extensions;
Object.defineProperties(this, {
message: {
value: message,
// By being enumerable, JSON.stringify will include `message` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: true,
writable: true
},
locations: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: _locations || undefined,
// By being enumerable, JSON.stringify will include `locations` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(_locations)
},
path: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: path || undefined,
// By being enumerable, JSON.stringify will include `path` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(path)
},
nodes: {
value: _nodes || undefined
},
source: {
value: _source || undefined
},
positions: {
value: _positions || undefined
},
originalError: {
value: originalError
},
extensions: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: _extensions || undefined,
// By being enumerable, JSON.stringify will include `path` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(_extensions)
}
}); // Include (non-enumerable) stack trace.
if (originalError && originalError.stack) {
Object.defineProperty(this, 'stack', {
value: originalError.stack,
writable: true,
configurable: true
});
} else if (Error.captureStackTrace) {
Error.captureStackTrace(this, GraphQLError);
} else {
Object.defineProperty(this, 'stack', {
value: Error().stack,
writable: true,
configurable: true
});
}
}
GraphQLError.prototype = Object.create(Error.prototype, {
constructor: {
value: GraphQLError
},
name: {
value: 'GraphQLError'
},
toString: {
value: function toString() {
return (0, _printError.printError)(this);
}
}
});
+220
View File
@@ -0,0 +1,220 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import { printError } from './printError';
import { getLocation } from '../language/location';
import type { SourceLocation } from '../language/location';
import type { ASTNode } from '../language/ast';
import type { Source } from '../language/source';
/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
* and stack trace, it also includes information about the locations in a
* GraphQL document and/or execution result that correspond to the Error.
*/
declare class GraphQLError extends Error {
constructor(
message: string,
nodes?: $ReadOnlyArray<ASTNode> | ASTNode | void,
source?: ?Source,
positions?: ?$ReadOnlyArray<number>,
path?: ?$ReadOnlyArray<string | number>,
originalError?: ?Error,
extensions?: ?{ [key: string]: mixed },
): void;
/**
* A message describing the Error for debugging purposes.
*
* Enumerable, and appears in the result of JSON.stringify().
*
* Note: should be treated as readonly, despite invariant usage.
*/
message: string;
/**
* An array of { line, column } locations within the source GraphQL document
* which correspond to this error.
*
* Errors during validation often contain multiple locations, for example to
* point out two things with the same name. Errors during execution include a
* single location, the field which produced the error.
*
* Enumerable, and appears in the result of JSON.stringify().
*/
+locations: $ReadOnlyArray<SourceLocation> | void;
/**
* An array describing the JSON-path into the execution response which
* corresponds to this error. Only included for errors during execution.
*
* Enumerable, and appears in the result of JSON.stringify().
*/
+path: $ReadOnlyArray<string | number> | void;
/**
* An array of GraphQL AST Nodes corresponding to this error.
*/
+nodes: $ReadOnlyArray<ASTNode> | void;
/**
* The source GraphQL document for the first location of this error.
*
* Note that if this Error represents more than one node, the source may not
* represent nodes after the first node.
*/
+source: Source | void;
/**
* An array of character offsets within the source GraphQL document
* which correspond to this error.
*/
+positions: $ReadOnlyArray<number> | void;
/**
* The original error thrown from a field resolver during execution.
*/
+originalError: ?Error;
/**
* Extension fields to add to the formatted error.
*/
+extensions: { [key: string]: mixed } | void;
}
export function GraphQLError( // eslint-disable-line no-redeclare
message: string,
nodes?: $ReadOnlyArray<ASTNode> | ASTNode | void,
source?: ?Source,
positions?: ?$ReadOnlyArray<number>,
path?: ?$ReadOnlyArray<string | number>,
originalError?: ?Error & { +extensions: mixed },
extensions?: ?{ [key: string]: mixed },
) {
// Compute list of blame nodes.
const _nodes = Array.isArray(nodes)
? nodes.length !== 0
? nodes
: undefined
: nodes
? [nodes]
: undefined;
// Compute locations in the source for the given nodes/positions.
let _source = source;
if (!_source && _nodes) {
const node = _nodes[0];
_source = node && node.loc && node.loc.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;
}
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 _extensions = extensions || (originalError && originalError.extensions);
Object.defineProperties(this, {
message: {
value: message,
// By being enumerable, JSON.stringify will include `message` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: true,
writable: true,
},
locations: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: _locations || undefined,
// By being enumerable, JSON.stringify will include `locations` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(_locations),
},
path: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: path || undefined,
// By being enumerable, JSON.stringify will include `path` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(path),
},
nodes: {
value: _nodes || undefined,
},
source: {
value: _source || undefined,
},
positions: {
value: _positions || undefined,
},
originalError: {
value: originalError,
},
extensions: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: _extensions || undefined,
// By being enumerable, JSON.stringify will include `path` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(_extensions),
},
});
// Include (non-enumerable) stack trace.
if (originalError && originalError.stack) {
Object.defineProperty(this, 'stack', {
value: originalError.stack,
writable: true,
configurable: true,
});
} else if (Error.captureStackTrace) {
Error.captureStackTrace(this, GraphQLError);
} else {
Object.defineProperty(this, 'stack', {
value: Error().stack,
writable: true,
configurable: true,
});
}
}
(GraphQLError: any).prototype = Object.create(Error.prototype, {
constructor: { value: GraphQLError },
name: { value: 'GraphQLError' },
toString: {
value: function toString() {
return printError(this);
},
},
});
+136
View File
@@ -0,0 +1,136 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
import { printError } from './printError';
import { getLocation } from '../language/location';
export function GraphQLError( // eslint-disable-line no-redeclare
message, nodes, source, positions, path, originalError, extensions) {
// Compute list of blame nodes.
var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions.
var _source = source;
if (!_source && _nodes) {
var node = _nodes[0];
_source = node && node.loc && node.loc.source;
}
var _positions = positions;
if (!_positions && _nodes) {
_positions = _nodes.reduce(function (list, node) {
if (node.loc) {
list.push(node.loc.start);
}
return list;
}, []);
}
if (_positions && _positions.length === 0) {
_positions = undefined;
}
var _locations;
if (positions && source) {
_locations = positions.map(function (pos) {
return getLocation(source, pos);
});
} else if (_nodes) {
_locations = _nodes.reduce(function (list, node) {
if (node.loc) {
list.push(getLocation(node.loc.source, node.loc.start));
}
return list;
}, []);
}
var _extensions = extensions || originalError && originalError.extensions;
Object.defineProperties(this, {
message: {
value: message,
// By being enumerable, JSON.stringify will include `message` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: true,
writable: true
},
locations: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: _locations || undefined,
// By being enumerable, JSON.stringify will include `locations` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(_locations)
},
path: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: path || undefined,
// By being enumerable, JSON.stringify will include `path` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(path)
},
nodes: {
value: _nodes || undefined
},
source: {
value: _source || undefined
},
positions: {
value: _positions || undefined
},
originalError: {
value: originalError
},
extensions: {
// Coercing falsey values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: _extensions || undefined,
// By being enumerable, JSON.stringify will include `path` in the
// resulting output. This ensures that the simplest possible GraphQL
// service adheres to the spec.
enumerable: Boolean(_extensions)
}
}); // Include (non-enumerable) stack trace.
if (originalError && originalError.stack) {
Object.defineProperty(this, 'stack', {
value: originalError.stack,
writable: true,
configurable: true
});
} else if (Error.captureStackTrace) {
Error.captureStackTrace(this, GraphQLError);
} else {
Object.defineProperty(this, 'stack', {
value: Error().stack,
writable: true,
configurable: true
});
}
}
GraphQLError.prototype = Object.create(Error.prototype, {
constructor: {
value: GraphQLError
},
name: {
value: 'GraphQLError'
},
toString: {
value: function toString() {
return printError(this);
}
}
});
+41
View File
@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.formatError = formatError;
var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
/**
* Given a GraphQLError, format it according to the rules described by the
* Response Format, Errors section of the GraphQL Specification.
*/
function formatError(error) {
!error ? (0, _invariant.default)(0, 'Received null or undefined error.') : void 0;
var message = error.message || 'An unknown error occurred.';
var locations = error.locations;
var path = error.path;
var extensions = error.extensions;
return extensions ? {
message: message,
locations: locations,
path: path,
extensions: extensions
} : {
message: message,
locations: locations,
path: path
};
}
+35
View File
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import invariant from '../jsutils/invariant';
import type { GraphQLError } from './GraphQLError';
import type { SourceLocation } from '../language/location';
/**
* Given a GraphQLError, format it according to the rules described by the
* Response Format, Errors section of the GraphQL Specification.
*/
export function formatError(error: GraphQLError): GraphQLFormattedError {
invariant(error, 'Received null or undefined error.');
const message = error.message || 'An unknown error occurred.';
const locations = error.locations;
const path = error.path;
const extensions = error.extensions;
return extensions
? { message, locations, path, extensions }
: { message, locations, path };
}
export type GraphQLFormattedError = {|
+message: string,
+locations: $ReadOnlyArray<SourceLocation> | void,
+path: $ReadOnlyArray<string | number> | void,
+extensions?: { [key: string]: mixed },
|};
+31
View File
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
import invariant from '../jsutils/invariant';
/**
* Given a GraphQLError, format it according to the rules described by the
* Response Format, Errors section of the GraphQL Specification.
*/
export function formatError(error) {
!error ? invariant(0, 'Received null or undefined error.') : void 0;
var message = error.message || 'An unknown error occurred.';
var locations = error.locations;
var path = error.path;
var extensions = error.extensions;
return extensions ? {
message: message,
locations: locations,
path: path,
extensions: extensions
} : {
message: message,
locations: locations,
path: path
};
}
+45
View File
@@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "GraphQLError", {
enumerable: true,
get: function get() {
return _GraphQLError.GraphQLError;
}
});
Object.defineProperty(exports, "syntaxError", {
enumerable: true,
get: function get() {
return _syntaxError.syntaxError;
}
});
Object.defineProperty(exports, "locatedError", {
enumerable: true,
get: function get() {
return _locatedError.locatedError;
}
});
Object.defineProperty(exports, "printError", {
enumerable: true,
get: function get() {
return _printError.printError;
}
});
Object.defineProperty(exports, "formatError", {
enumerable: true,
get: function get() {
return _formatError.formatError;
}
});
var _GraphQLError = require("./GraphQLError");
var _syntaxError = require("./syntaxError");
var _locatedError = require("./locatedError");
var _printError = require("./printError");
var _formatError = require("./formatError");
+16
View File
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
export { GraphQLError } from './GraphQLError';
export { syntaxError } from './syntaxError';
export { locatedError } from './locatedError';
export { printError } from './printError';
export { formatError } from './formatError';
export type { GraphQLFormattedError } from './formatError';
+13
View File
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
export { GraphQLError } from './GraphQLError';
export { syntaxError } from './syntaxError';
export { locatedError } from './locatedError';
export { printError } from './printError';
export { formatError } from './formatError';
+32
View File
@@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.locatedError = locatedError;
var _GraphQLError = require("./GraphQLError");
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
/**
* Given an arbitrary Error, presumably thrown while attempting to execute a
* GraphQL operation, produce a new GraphQLError aware of the location in the
* document responsible for the original Error.
*/
function locatedError(originalError, nodes, path) {
// Note: this uses a brand-check to support GraphQL errors originating from
// other contexts.
if (originalError && Array.isArray(originalError.path)) {
return originalError;
}
return new _GraphQLError.GraphQLError(originalError && originalError.message, originalError && originalError.nodes || nodes, originalError && originalError.source, originalError && originalError.positions, path, originalError);
}
+37
View File
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import { GraphQLError } from './GraphQLError';
import type { ASTNode } from '../language/ast';
/**
* Given an arbitrary Error, presumably thrown while attempting to execute a
* GraphQL operation, produce a new GraphQLError aware of the location in the
* document responsible for the original Error.
*/
export function locatedError(
originalError: Error | GraphQLError,
nodes: $ReadOnlyArray<ASTNode>,
path: $ReadOnlyArray<string | number>,
): GraphQLError {
// Note: this uses a brand-check to support GraphQL errors originating from
// other contexts.
if (originalError && Array.isArray(originalError.path)) {
return (originalError: any);
}
return new GraphQLError(
originalError && originalError.message,
(originalError && (originalError: any).nodes) || nodes,
originalError && (originalError: any).source,
originalError && (originalError: any).positions,
path,
originalError,
);
}
+24
View File
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
import { GraphQLError } from './GraphQLError';
/**
* Given an arbitrary Error, presumably thrown while attempting to execute a
* GraphQL operation, produce a new GraphQLError aware of the location in the
* document responsible for the original Error.
*/
export function locatedError(originalError, nodes, path) {
// Note: this uses a brand-check to support GraphQL errors originating from
// other contexts.
if (originalError && Array.isArray(originalError.path)) {
return originalError;
}
return new GraphQLError(originalError && originalError.message, originalError && originalError.nodes || nodes, originalError && originalError.source, originalError && originalError.positions, path, originalError);
}
+146
View File
@@ -0,0 +1,146 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.printError = printError;
var _location = require("../language/location");
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*/
function printError(error) {
var printedLocations = [];
if (error.nodes) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = error.nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var node = _step.value;
if (node.loc) {
printedLocations.push(highlightSourceAtLocation(node.loc.source, (0, _location.getLocation)(node.loc.source, node.loc.start)));
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
} else if (error.source && error.locations) {
var source = error.source;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = error.locations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var location = _step2.value;
printedLocations.push(highlightSourceAtLocation(source, location));
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
return printedLocations.length === 0 ? error.message : [error.message].concat(printedLocations).join('\n\n') + '\n';
}
/**
* Render a helpful description of the location of the error in the GraphQL
* Source document.
*/
function highlightSourceAtLocation(source, location) {
var firstLineColumnOffset = source.locationOffset.column - 1;
var body = whitespace(firstLineColumnOffset) + source.body;
var lineIndex = location.line - 1;
var lineOffset = source.locationOffset.line - 1;
var lineNum = location.line + lineOffset;
var columnOffset = location.line === 1 ? firstLineColumnOffset : 0;
var columnNum = location.column + columnOffset;
var lines = body.split(/\r\n|[\n\r]/g);
return "".concat(source.name, " (").concat(lineNum, ":").concat(columnNum, ")\n") + printPrefixedLines([// Lines specified like this: ["prefix", "string"],
["".concat(lineNum - 1, ": "), lines[lineIndex - 1]], ["".concat(lineNum, ": "), lines[lineIndex]], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1, ": "), lines[lineIndex + 1]]]);
}
function printPrefixedLines(lines) {
var existingLines = lines.filter(function (_ref) {
var _ = _ref[0],
line = _ref[1];
return line !== undefined;
});
var padLen = 0;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = existingLines[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var _ref4 = _step3.value;
var prefix = _ref4[0];
padLen = Math.max(padLen, prefix.length);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return existingLines.map(function (_ref3) {
var prefix = _ref3[0],
line = _ref3[1];
return lpad(padLen, prefix) + line;
}).join('\n');
}
function whitespace(len) {
return Array(len + 1).join(' ');
}
function lpad(len, str) {
return whitespace(len - str.length) + str;
}
+93
View File
@@ -0,0 +1,93 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import { getLocation } from '../language/location';
import type { SourceLocation } from '../language/location';
import type { Source } from '../language/source';
import type { GraphQLError } from './GraphQLError';
/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*/
export function printError(error: GraphQLError): string {
const printedLocations = [];
if (error.nodes) {
for (const node of error.nodes) {
if (node.loc) {
printedLocations.push(
highlightSourceAtLocation(
node.loc.source,
getLocation(node.loc.source, node.loc.start),
),
);
}
}
} else if (error.source && error.locations) {
const source = error.source;
for (const location of error.locations) {
printedLocations.push(highlightSourceAtLocation(source, location));
}
}
return printedLocations.length === 0
? error.message
: [error.message, ...printedLocations].join('\n\n') + '\n';
}
/**
* Render a helpful description of the location of the error in the GraphQL
* Source document.
*/
function highlightSourceAtLocation(
source: Source,
location: SourceLocation,
): string {
const firstLineColumnOffset = source.locationOffset.column - 1;
const body = whitespace(firstLineColumnOffset) + source.body;
const lineIndex = location.line - 1;
const lineOffset = source.locationOffset.line - 1;
const lineNum = location.line + lineOffset;
const columnOffset = location.line === 1 ? firstLineColumnOffset : 0;
const columnNum = location.column + columnOffset;
const lines = body.split(/\r\n|[\n\r]/g);
return (
`${source.name} (${lineNum}:${columnNum})\n` +
printPrefixedLines([
// Lines specified like this: ["prefix", "string"],
[`${lineNum - 1}: `, lines[lineIndex - 1]],
[`${lineNum}: `, lines[lineIndex]],
['', whitespace(columnNum - 1) + '^'],
[`${lineNum + 1}: `, lines[lineIndex + 1]],
])
);
}
function printPrefixedLines(lines: Array<[string, string]>): string {
const existingLines = lines.filter(([_, line]) => line !== undefined);
let padLen = 0;
for (const [prefix] of existingLines) {
padLen = Math.max(padLen, prefix.length);
}
return existingLines
.map(([prefix, line]) => lpad(padLen, prefix) + line)
.join('\n');
}
function whitespace(len: number): string {
return Array(len + 1).join(' ');
}
function lpad(len: number, str: string): string {
return whitespace(len - str.length) + str;
}
+137
View File
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
import { getLocation } from '../language/location';
/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*/
export function printError(error) {
var printedLocations = [];
if (error.nodes) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = error.nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var node = _step.value;
if (node.loc) {
printedLocations.push(highlightSourceAtLocation(node.loc.source, getLocation(node.loc.source, node.loc.start)));
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
} else if (error.source && error.locations) {
var source = error.source;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = error.locations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var location = _step2.value;
printedLocations.push(highlightSourceAtLocation(source, location));
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
return printedLocations.length === 0 ? error.message : [error.message].concat(printedLocations).join('\n\n') + '\n';
}
/**
* Render a helpful description of the location of the error in the GraphQL
* Source document.
*/
function highlightSourceAtLocation(source, location) {
var firstLineColumnOffset = source.locationOffset.column - 1;
var body = whitespace(firstLineColumnOffset) + source.body;
var lineIndex = location.line - 1;
var lineOffset = source.locationOffset.line - 1;
var lineNum = location.line + lineOffset;
var columnOffset = location.line === 1 ? firstLineColumnOffset : 0;
var columnNum = location.column + columnOffset;
var lines = body.split(/\r\n|[\n\r]/g);
return "".concat(source.name, " (").concat(lineNum, ":").concat(columnNum, ")\n") + printPrefixedLines([// Lines specified like this: ["prefix", "string"],
["".concat(lineNum - 1, ": "), lines[lineIndex - 1]], ["".concat(lineNum, ": "), lines[lineIndex]], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1, ": "), lines[lineIndex + 1]]]);
}
function printPrefixedLines(lines) {
var existingLines = lines.filter(function (_ref) {
var _ = _ref[0],
line = _ref[1];
return line !== undefined;
});
var padLen = 0;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = existingLines[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var _ref4 = _step3.value;
var prefix = _ref4[0];
padLen = Math.max(padLen, prefix.length);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return existingLines.map(function (_ref3) {
var prefix = _ref3[0],
line = _ref3[1];
return lpad(padLen, prefix) + line;
}).join('\n');
}
function whitespace(len) {
return Array(len + 1).join(' ');
}
function lpad(len, str) {
return whitespace(len - str.length) + str;
}
+25
View File
@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.syntaxError = syntaxError;
var _GraphQLError = require("./GraphQLError");
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
/**
* Produces a GraphQLError representing a syntax error, containing useful
* descriptive information about the syntax error's position in the source.
*/
function syntaxError(source, position, description) {
return new _GraphQLError.GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]);
}
+25
View File
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import type { Source } from '../language/source';
import { GraphQLError } from './GraphQLError';
/**
* Produces a GraphQLError representing a syntax error, containing useful
* descriptive information about the syntax error's position in the source.
*/
export function syntaxError(
source: Source,
position: number,
description: string,
): GraphQLError {
return new GraphQLError(`Syntax Error: ${description}`, undefined, source, [
position,
]);
}
+17
View File
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* strict
*/
import { GraphQLError } from './GraphQLError';
/**
* Produces a GraphQLError representing a syntax error, containing useful
* descriptive information about the syntax error's position in the source.
*/
export function syntaxError(source, position, description) {
return new GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]);
}