relay-link-ssr
Version:
Relay Link for Server-Side Rendering
1,685 lines (1,334 loc) • 90.1 kB
JavaScript
import { __extends } from 'tslib';
import { OperationKind, RelayLink } from 'relay-link';
import { Observable } from 'relay-runtime';
var SsrPendingLink = (function (_super) {
__extends(SsrPendingLink, _super);
function SsrPendingLink() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.promiseMap = {};
return _this;
}
SsrPendingLink.prototype.request = function (operation, forward) {
var key = operation.getUniqueKey();
var observable = forward(operation);
if (!this.promiseMap[key] && operation.operationKind === OperationKind.QUERY) {
this.promiseMap[key] = observable.toPromise();
}
return observable;
};
SsrPendingLink.prototype.asMap = function () {
return this.promiseMap;
};
SsrPendingLink.prototype.getPending = function () {
return Promise.all(Object.values(this.promiseMap));
};
return SsrPendingLink;
}(RelayLink));
function _typeof$2(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof$2 = function _typeof(obj) { return typeof obj; }; } else { _typeof$2 = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof$2(obj); }
/**
* Return true if `value` is object-like. A value is object-like if it's not
* `null` and has a `typeof` result of "object".
*/
function isObjectLike(value) {
return _typeof$2(value) == 'object' && value !== null;
}
// In ES2015 (or a polyfilled) environment, this will be Symbol.iterator
var SYMBOL_TO_STRING_TAG = typeof Symbol === 'function' && Symbol.toStringTag != null ? Symbol.toStringTag : '@@toStringTag';
/**
* Represents a location in a Source.
*/
/**
* Takes a Source and a UTF-8 character offset, and returns the corresponding
* line and column as a SourceLocation.
*/
function getLocation(source, position) {
var lineRegexp = /\r\n|[\n\r]/g;
var line = 1;
var column = position + 1;
var match;
while ((match = lineRegexp.exec(source.body)) && match.index < position) {
line += 1;
column = position + 1 - (match.index + match[0].length);
}
return {
line: line,
column: column
};
}
/**
* Render a helpful description of the location in the GraphQL Source document.
*/
function printLocation(location) {
return printSourceLocation(location.source, getLocation(location.source, location.start));
}
/**
* Render a helpful description of the location in the GraphQL Source document.
*/
function printSourceLocation(source, sourceLocation) {
var firstLineColumnOffset = source.locationOffset.column - 1;
var body = whitespace(firstLineColumnOffset) + source.body;
var lineIndex = sourceLocation.line - 1;
var lineOffset = source.locationOffset.line - 1;
var lineNum = sourceLocation.line + lineOffset;
var columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
var columnNum = sourceLocation.column + columnOffset;
var locationStr = "".concat(source.name, ":").concat(lineNum, ":").concat(columnNum, "\n");
var lines = body.split(/\r\n|[\n\r]/g);
var locationLine = lines[lineIndex]; // Special case for minified documents
if (locationLine.length > 120) {
var subLineIndex = Math.floor(columnNum / 80);
var subLineColumnNum = columnNum % 80;
var subLines = [];
for (var i = 0; i < locationLine.length; i += 80) {
subLines.push(locationLine.slice(i, i + 80));
}
return locationStr + printPrefixedLines([["".concat(lineNum), subLines[0]]].concat(subLines.slice(1, subLineIndex + 1).map(function (subLine) {
return ['', subLine];
}), [[' ', whitespace(subLineColumnNum - 1) + '^'], ['', subLines[subLineIndex + 1]]]));
}
return locationStr + printPrefixedLines([// Lines specified like this: ["prefix", "string"],
["".concat(lineNum - 1), lines[lineIndex - 1]], ["".concat(lineNum), locationLine], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1), lines[lineIndex + 1]]]);
}
function printPrefixedLines(lines) {
var existingLines = lines.filter(function (_ref) {
_ref[0];
var line = _ref[1];
return line !== undefined;
});
var padLen = Math.max.apply(Math, existingLines.map(function (_ref2) {
var prefix = _ref2[0];
return prefix.length;
}));
return existingLines.map(function (_ref3) {
var prefix = _ref3[0],
line = _ref3[1];
return leftPad(padLen, prefix) + (line ? ' | ' + line : ' |');
}).join('\n');
}
function whitespace(len) {
return Array(len + 1).join(' ');
}
function leftPad(len, str) {
return whitespace(len - str.length) + str;
}
function _typeof$1(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof$1 = function _typeof(obj) { return typeof obj; }; } else { _typeof$1 = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof$1(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties$1(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass$1(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$1(Constructor.prototype, protoProps); if (staticProps) _defineProperties$1(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof$1(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/**
* 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.
*/
var GraphQLError = /*#__PURE__*/function (_Error) {
_inherits(GraphQLError, _Error);
var _super = _createSuper(GraphQLError);
/**
* 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.
*/
/**
* 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().
*/
/**
* 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().
*/
/**
* An array of GraphQL AST Nodes corresponding to this error.
*/
/**
* 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.
*/
/**
* An array of character offsets within the source GraphQL document
* which correspond to this error.
*/
/**
* The original error thrown from a field resolver during execution.
*/
/**
* Extension fields to add to the formatted error.
*/
function GraphQLError(message, nodes, source, positions, path, originalError, extensions) {
var _locations2, _source2, _positions2, _extensions2;
var _this;
_classCallCheck(this, GraphQLError);
_this = _super.call(this, message); // 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 _nodes$0$loc;
_source = (_nodes$0$loc = _nodes[0].loc) === null || _nodes$0$loc === void 0 ? void 0 : _nodes$0$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;
if (_extensions == null && originalError != null) {
var originalExtensions = originalError.extensions;
if (isObjectLike(originalExtensions)) {
_extensions = originalExtensions;
}
}
Object.defineProperties(_assertThisInitialized(_this), {
name: {
value: 'GraphQLError'
},
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 falsy values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: (_locations2 = _locations) !== null && _locations2 !== void 0 ? _locations2 : 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: _locations != null
},
path: {
// Coercing falsy values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: path !== null && path !== void 0 ? 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: path != null
},
nodes: {
value: _nodes !== null && _nodes !== void 0 ? _nodes : undefined
},
source: {
value: (_source2 = _source) !== null && _source2 !== void 0 ? _source2 : undefined
},
positions: {
value: (_positions2 = _positions) !== null && _positions2 !== void 0 ? _positions2 : undefined
},
originalError: {
value: originalError
},
extensions: {
// Coercing falsy values to undefined ensures they will not be included
// in JSON.stringify() when not provided.
value: (_extensions2 = _extensions) !== null && _extensions2 !== void 0 ? _extensions2 : 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: _extensions != null
}
}); // Include (non-enumerable) stack trace.
if (originalError !== null && originalError !== void 0 && originalError.stack) {
Object.defineProperty(_assertThisInitialized(_this), 'stack', {
value: originalError.stack,
writable: true,
configurable: true
});
return _possibleConstructorReturn(_this);
} // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
if (Error.captureStackTrace) {
Error.captureStackTrace(_assertThisInitialized(_this), GraphQLError);
} else {
Object.defineProperty(_assertThisInitialized(_this), 'stack', {
value: Error().stack,
writable: true,
configurable: true
});
}
return _this;
}
_createClass$1(GraphQLError, [{
key: "toString",
value: function toString() {
return printError(this);
} // FIXME: workaround to not break chai comparisons, should be remove in v16
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
}, {
key: SYMBOL_TO_STRING_TAG,
get: function get() {
return 'Object';
}
}]);
return GraphQLError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*/
function printError(error) {
var output = error.message;
if (error.nodes) {
for (var _i2 = 0, _error$nodes2 = error.nodes; _i2 < _error$nodes2.length; _i2++) {
var node = _error$nodes2[_i2];
if (node.loc) {
output += '\n\n' + printLocation(node.loc);
}
}
} else if (error.source && error.locations) {
for (var _i4 = 0, _error$locations2 = error.locations; _i4 < _error$locations2.length; _i4++) {
var location = _error$locations2[_i4];
output += '\n\n' + printSourceLocation(error.source, location);
}
}
return output;
}
/**
* 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("Syntax Error: ".concat(description), undefined, source, [position]);
}
/**
* The set of allowed kind values for AST nodes.
*/
var Kind = Object.freeze({
// Name
NAME: 'Name',
// Document
DOCUMENT: 'Document',
OPERATION_DEFINITION: 'OperationDefinition',
VARIABLE_DEFINITION: 'VariableDefinition',
SELECTION_SET: 'SelectionSet',
FIELD: 'Field',
ARGUMENT: 'Argument',
// Fragments
FRAGMENT_SPREAD: 'FragmentSpread',
INLINE_FRAGMENT: 'InlineFragment',
FRAGMENT_DEFINITION: 'FragmentDefinition',
// Values
VARIABLE: 'Variable',
INT: 'IntValue',
FLOAT: 'FloatValue',
STRING: 'StringValue',
BOOLEAN: 'BooleanValue',
NULL: 'NullValue',
ENUM: 'EnumValue',
LIST: 'ListValue',
OBJECT: 'ObjectValue',
OBJECT_FIELD: 'ObjectField',
// Directives
DIRECTIVE: 'Directive',
// Types
NAMED_TYPE: 'NamedType',
LIST_TYPE: 'ListType',
NON_NULL_TYPE: 'NonNullType',
// Type System Definitions
SCHEMA_DEFINITION: 'SchemaDefinition',
OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition',
// Type Definitions
SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition',
OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition',
FIELD_DEFINITION: 'FieldDefinition',
INPUT_VALUE_DEFINITION: 'InputValueDefinition',
INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition',
UNION_TYPE_DEFINITION: 'UnionTypeDefinition',
ENUM_TYPE_DEFINITION: 'EnumTypeDefinition',
ENUM_VALUE_DEFINITION: 'EnumValueDefinition',
INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition',
// Directive Definitions
DIRECTIVE_DEFINITION: 'DirectiveDefinition',
// Type System Extensions
SCHEMA_EXTENSION: 'SchemaExtension',
// Type Extensions
SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension',
OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension',
INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension',
UNION_TYPE_EXTENSION: 'UnionTypeExtension',
ENUM_TYPE_EXTENSION: 'EnumTypeExtension',
INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension'
});
/**
* The enum type representing the possible kind values of AST nodes.
*/
function invariant(condition, message) {
var booleanCondition = Boolean(condition); // istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
if (!booleanCondition) {
throw new Error(message != null ? message : 'Unexpected invariant triggered.');
}
}
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
/**
* The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
*/
function defineInspect(classObject) {
var fn = classObject.prototype.toJSON;
typeof fn === 'function' || invariant(0);
classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
if (nodejsCustomInspectSymbol) {
classObject.prototype[nodejsCustomInspectSymbol] = fn;
}
}
/**
* Contains a range of UTF-8 character offsets and token references that
* identify the region of the source from which the AST derived.
*/
var Location = /*#__PURE__*/function () {
/**
* The character offset at which this Node begins.
*/
/**
* The character offset at which this Node ends.
*/
/**
* The Token at which this Node begins.
*/
/**
* The Token at which this Node ends.
*/
/**
* The Source document the AST represents.
*/
function Location(startToken, endToken, source) {
this.start = startToken.start;
this.end = endToken.end;
this.startToken = startToken;
this.endToken = endToken;
this.source = source;
}
var _proto = Location.prototype;
_proto.toJSON = function toJSON() {
return {
start: this.start,
end: this.end
};
};
return Location;
}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(Location);
/**
* Represents a range of characters represented by a lexical token
* within a Source.
*/
var Token = /*#__PURE__*/function () {
/**
* The kind of Token.
*/
/**
* The character offset at which this Node begins.
*/
/**
* The character offset at which this Node ends.
*/
/**
* The 1-indexed line number on which this Token appears.
*/
/**
* The 1-indexed column number at which this Token begins.
*/
/**
* For non-punctuation tokens, represents the interpreted value of the token.
*/
/**
* Tokens exist as nodes in a double-linked-list amongst all tokens
* including ignored tokens. <SOF> is always the first node and <EOF>
* the last.
*/
function Token(kind, start, end, line, column, prev, value) {
this.kind = kind;
this.start = start;
this.end = end;
this.line = line;
this.column = column;
this.value = value;
this.prev = prev;
this.next = null;
}
var _proto2 = Token.prototype;
_proto2.toJSON = function toJSON() {
return {
kind: this.kind,
value: this.value,
line: this.line,
column: this.column
};
};
return Token;
}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(Token);
/**
* The list of all possible AST node types.
*/
/**
* An exported enum describing the different kinds of tokens that the
* lexer emits.
*/
var TokenKind = Object.freeze({
SOF: '<SOF>',
EOF: '<EOF>',
BANG: '!',
DOLLAR: '$',
AMP: '&',
PAREN_L: '(',
PAREN_R: ')',
SPREAD: '...',
COLON: ':',
EQUALS: '=',
AT: '@',
BRACKET_L: '[',
BRACKET_R: ']',
BRACE_L: '{',
PIPE: '|',
BRACE_R: '}',
NAME: 'Name',
INT: 'Int',
FLOAT: 'Float',
STRING: 'String',
BLOCK_STRING: 'BlockString',
COMMENT: 'Comment'
});
/**
* The enum type representing the token kinds values.
*/
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var MAX_ARRAY_LENGTH = 10;
var MAX_RECURSIVE_DEPTH = 2;
/**
* Used to print values in error messages.
*/
function inspect(value) {
return formatValue(value, []);
}
function formatValue(value, seenValues) {
switch (_typeof(value)) {
case 'string':
return JSON.stringify(value);
case 'function':
return value.name ? "[function ".concat(value.name, "]") : '[function]';
case 'object':
if (value === null) {
return 'null';
}
return formatObjectValue(value, seenValues);
default:
return String(value);
}
}
function formatObjectValue(value, previouslySeenValues) {
if (previouslySeenValues.indexOf(value) !== -1) {
return '[Circular]';
}
var seenValues = [].concat(previouslySeenValues, [value]);
var customInspectFn = getCustomFn(value);
if (customInspectFn !== undefined) {
var customValue = customInspectFn.call(value); // check for infinite recursion
if (customValue !== value) {
return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
}
} else if (Array.isArray(value)) {
return formatArray(value, seenValues);
}
return formatObject(value, seenValues);
}
function formatObject(object, seenValues) {
var keys = Object.keys(object);
if (keys.length === 0) {
return '{}';
}
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
return '[' + getObjectTag(object) + ']';
}
var properties = keys.map(function (key) {
var value = formatValue(object[key], seenValues);
return key + ': ' + value;
});
return '{ ' + properties.join(', ') + ' }';
}
function formatArray(array, seenValues) {
if (array.length === 0) {
return '[]';
}
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
return '[Array]';
}
var len = Math.min(MAX_ARRAY_LENGTH, array.length);
var remaining = array.length - len;
var items = [];
for (var i = 0; i < len; ++i) {
items.push(formatValue(array[i], seenValues));
}
if (remaining === 1) {
items.push('... 1 more item');
} else if (remaining > 1) {
items.push("... ".concat(remaining, " more items"));
}
return '[' + items.join(', ') + ']';
}
function getCustomFn(object) {
var customInspectFn = object[String(nodejsCustomInspectSymbol)];
if (typeof customInspectFn === 'function') {
return customInspectFn;
}
if (typeof object.inspect === 'function') {
return object.inspect;
}
}
function getObjectTag(object) {
var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
if (tag === 'Object' && typeof object.constructor === 'function') {
var name = object.constructor.name;
if (typeof name === 'string' && name !== '') {
return name;
}
}
return tag;
}
function devAssert(condition, message) {
var booleanCondition = Boolean(condition); // istanbul ignore else (See transformation done in './resources/inlineInvariant.js')
if (!booleanCondition) {
throw new Error(message);
}
}
/**
* A replacement for instanceof which includes an error warning when multi-realm
* constructors are detected.
*/
// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
// See: https://webpack.js.org/guides/production/
var instanceOf = process.env.NODE_ENV === 'production' ? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
// eslint-disable-next-line no-shadow
function instanceOf(value, constructor) {
return value instanceof constructor;
} : // eslint-disable-next-line no-shadow
function instanceOf(value, constructor) {
if (value instanceof constructor) {
return true;
}
if (value) {
var valueClass = value.constructor;
var className = constructor.name;
if (className && valueClass && valueClass.name === className) {
throw new Error("Cannot use ".concat(className, " \"").concat(value, "\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results."));
}
}
return false;
};
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/**
* A representation of source input to GraphQL. The `name` and `locationOffset` parameters are
* optional, but they are useful for clients who store GraphQL documents in source files.
* For example, if the GraphQL input starts at line 40 in a file named `Foo.graphql`, it might
* be useful for `name` to be `"Foo.graphql"` and location to be `{ line: 40, column: 1 }`.
* The `line` and `column` properties in `locationOffset` are 1-indexed.
*/
var Source = /*#__PURE__*/function () {
function Source(body) {
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'GraphQL request';
var locationOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
line: 1,
column: 1
};
typeof body === 'string' || devAssert(0, "Body must be a string. Received: ".concat(inspect(body), "."));
this.body = body;
this.name = name;
this.locationOffset = locationOffset;
this.locationOffset.line > 0 || devAssert(0, 'line in locationOffset is 1-indexed and must be positive.');
this.locationOffset.column > 0 || devAssert(0, 'column in locationOffset is 1-indexed and must be positive.');
} // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
_createClass(Source, [{
key: SYMBOL_TO_STRING_TAG,
get: function get() {
return 'Source';
}
}]);
return Source;
}();
/**
* Test if the given value is a Source object.
*
* @internal
*/
// eslint-disable-next-line no-redeclare
function isSource(source) {
return instanceOf(source, Source);
}
/**
* The set of allowed directive location values.
*/
var DirectiveLocation = Object.freeze({
// Request Definitions
QUERY: 'QUERY',
MUTATION: 'MUTATION',
SUBSCRIPTION: 'SUBSCRIPTION',
FIELD: 'FIELD',
FRAGMENT_DEFINITION: 'FRAGMENT_DEFINITION',
FRAGMENT_SPREAD: 'FRAGMENT_SPREAD',
INLINE_FRAGMENT: 'INLINE_FRAGMENT',
VARIABLE_DEFINITION: 'VARIABLE_DEFINITION',
// Type System Definitions
SCHEMA: 'SCHEMA',
SCALAR: 'SCALAR',
OBJECT: 'OBJECT',
FIELD_DEFINITION: 'FIELD_DEFINITION',
ARGUMENT_DEFINITION: 'ARGUMENT_DEFINITION',
INTERFACE: 'INTERFACE',
UNION: 'UNION',
ENUM: 'ENUM',
ENUM_VALUE: 'ENUM_VALUE',
INPUT_OBJECT: 'INPUT_OBJECT',
INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION'
});
/**
* The enum type representing the directive location values.
*/
/**
* Produces the value of a block string from its parsed raw value, similar to
* CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.
*
* This implements the GraphQL spec's BlockStringValue() static algorithm.
*
* @internal
*/
function dedentBlockStringValue(rawString) {
// Expand a block string's raw value into independent lines.
var lines = rawString.split(/\r\n|[\n\r]/g); // Remove common indentation from all lines but first.
var commonIndent = getBlockStringIndentation(rawString);
if (commonIndent !== 0) {
for (var i = 1; i < lines.length; i++) {
lines[i] = lines[i].slice(commonIndent);
}
} // Remove leading and trailing blank lines.
var startLine = 0;
while (startLine < lines.length && isBlank(lines[startLine])) {
++startLine;
}
var endLine = lines.length;
while (endLine > startLine && isBlank(lines[endLine - 1])) {
--endLine;
} // Return a string of the lines joined with U+000A.
return lines.slice(startLine, endLine).join('\n');
}
function isBlank(str) {
for (var i = 0; i < str.length; ++i) {
if (str[i] !== ' ' && str[i] !== '\t') {
return false;
}
}
return true;
}
/**
* @internal
*/
function getBlockStringIndentation(value) {
var _commonIndent;
var isFirstLine = true;
var isEmptyLine = true;
var indent = 0;
var commonIndent = null;
for (var i = 0; i < value.length; ++i) {
switch (value.charCodeAt(i)) {
case 13:
// \r
if (value.charCodeAt(i + 1) === 10) {
++i; // skip \r\n as one symbol
}
// falls through
case 10:
// \n
isFirstLine = false;
isEmptyLine = true;
indent = 0;
break;
case 9: // \t
case 32:
// <space>
++indent;
break;
default:
if (isEmptyLine && !isFirstLine && (commonIndent === null || indent < commonIndent)) {
commonIndent = indent;
}
isEmptyLine = false;
}
}
return (_commonIndent = commonIndent) !== null && _commonIndent !== void 0 ? _commonIndent : 0;
}
/**
* Given a Source object, creates a Lexer for that source.
* A Lexer is a stateful stream generator in that every time
* it is advanced, it returns the next token in the Source. Assuming the
* source lexes, the final Token emitted by the lexer will be of kind
* EOF, after which the lexer will repeatedly return the same EOF token
* whenever called.
*/
var Lexer = /*#__PURE__*/function () {
/**
* The previously focused non-ignored token.
*/
/**
* The currently focused non-ignored token.
*/
/**
* The (1-indexed) line containing the current token.
*/
/**
* The character offset at which the current line begins.
*/
function Lexer(source) {
var startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0, null);
this.source = source;
this.lastToken = startOfFileToken;
this.token = startOfFileToken;
this.line = 1;
this.lineStart = 0;
}
/**
* Advances the token stream to the next non-ignored token.
*/
var _proto = Lexer.prototype;
_proto.advance = function advance() {
this.lastToken = this.token;
var token = this.token = this.lookahead();
return token;
}
/**
* Looks ahead and returns the next non-ignored token, but does not change
* the state of Lexer.
*/
;
_proto.lookahead = function lookahead() {
var token = this.token;
if (token.kind !== TokenKind.EOF) {
do {
var _token$next;
// Note: next is only mutable during parsing, so we cast to allow this.
token = (_token$next = token.next) !== null && _token$next !== void 0 ? _token$next : token.next = readToken(this, token);
} while (token.kind === TokenKind.COMMENT);
}
return token;
};
return Lexer;
}();
/**
* @internal
*/
function isPunctuatorTokenKind(kind) {
return kind === TokenKind.BANG || kind === TokenKind.DOLLAR || kind === TokenKind.AMP || kind === TokenKind.PAREN_L || kind === TokenKind.PAREN_R || kind === TokenKind.SPREAD || kind === TokenKind.COLON || kind === TokenKind.EQUALS || kind === TokenKind.AT || kind === TokenKind.BRACKET_L || kind === TokenKind.BRACKET_R || kind === TokenKind.BRACE_L || kind === TokenKind.PIPE || kind === TokenKind.BRACE_R;
}
function printCharCode(code) {
return (// NaN/undefined represents access beyond the end of the file.
isNaN(code) ? TokenKind.EOF : // Trust JSON for ASCII.
code < 0x007f ? JSON.stringify(String.fromCharCode(code)) : // Otherwise print the escaped form.
"\"\\u".concat(('00' + code.toString(16).toUpperCase()).slice(-4), "\"")
);
}
/**
* Gets the next token from the source starting at the given position.
*
* This skips over whitespace until it finds the next lexable token, then lexes
* punctuators immediately or calls the appropriate helper function for more
* complicated tokens.
*/
function readToken(lexer, prev) {
var source = lexer.source;
var body = source.body;
var bodyLength = body.length;
var pos = prev.end;
while (pos < bodyLength) {
var code = body.charCodeAt(pos);
var _line = lexer.line;
var _col = 1 + pos - lexer.lineStart; // SourceCharacter
switch (code) {
case 0xfeff: // <BOM>
case 9: // \t
case 32: // <space>
case 44:
// ,
++pos;
continue;
case 10:
// \n
++pos;
++lexer.line;
lexer.lineStart = pos;
continue;
case 13:
// \r
if (body.charCodeAt(pos + 1) === 10) {
pos += 2;
} else {
++pos;
}
++lexer.line;
lexer.lineStart = pos;
continue;
case 33:
// !
return new Token(TokenKind.BANG, pos, pos + 1, _line, _col, prev);
case 35:
// #
return readComment(source, pos, _line, _col, prev);
case 36:
// $
return new Token(TokenKind.DOLLAR, pos, pos + 1, _line, _col, prev);
case 38:
// &
return new Token(TokenKind.AMP, pos, pos + 1, _line, _col, prev);
case 40:
// (
return new Token(TokenKind.PAREN_L, pos, pos + 1, _line, _col, prev);
case 41:
// )
return new Token(TokenKind.PAREN_R, pos, pos + 1, _line, _col, prev);
case 46:
// .
if (body.charCodeAt(pos + 1) === 46 && body.charCodeAt(pos + 2) === 46) {
return new Token(TokenKind.SPREAD, pos, pos + 3, _line, _col, prev);
}
break;
case 58:
// :
return new Token(TokenKind.COLON, pos, pos + 1, _line, _col, prev);
case 61:
// =
return new Token(TokenKind.EQUALS, pos, pos + 1, _line, _col, prev);
case 64:
// @
return new Token(TokenKind.AT, pos, pos + 1, _line, _col, prev);
case 91:
// [
return new Token(TokenKind.BRACKET_L, pos, pos + 1, _line, _col, prev);
case 93:
// ]
return new Token(TokenKind.BRACKET_R, pos, pos + 1, _line, _col, prev);
case 123:
// {
return new Token(TokenKind.BRACE_L, pos, pos + 1, _line, _col, prev);
case 124:
// |
return new Token(TokenKind.PIPE, pos, pos + 1, _line, _col, prev);
case 125:
// }
return new Token(TokenKind.BRACE_R, pos, pos + 1, _line, _col, prev);
case 34:
// "
if (body.charCodeAt(pos + 1) === 34 && body.charCodeAt(pos + 2) === 34) {
return readBlockString(source, pos, _line, _col, prev, lexer);
}
return readString(source, pos, _line, _col, prev);
case 45: // -
case 48: // 0
case 49: // 1
case 50: // 2
case 51: // 3
case 52: // 4
case 53: // 5
case 54: // 6
case 55: // 7
case 56: // 8
case 57:
// 9
return readNumber(source, pos, code, _line, _col, prev);
case 65: // A
case 66: // B
case 67: // C
case 68: // D
case 69: // E
case 70: // F
case 71: // G
case 72: // H
case 73: // I
case 74: // J
case 75: // K
case 76: // L
case 77: // M
case 78: // N
case 79: // O
case 80: // P
case 81: // Q
case 82: // R
case 83: // S
case 84: // T
case 85: // U
case 86: // V
case 87: // W
case 88: // X
case 89: // Y
case 90: // Z
case 95: // _
case 97: // a
case 98: // b
case 99: // c
case 100: // d
case 101: // e
case 102: // f
case 103: // g
case 104: // h
case 105: // i
case 106: // j
case 107: // k
case 108: // l
case 109: // m
case 110: // n
case 111: // o
case 112: // p
case 113: // q
case 114: // r
case 115: // s
case 116: // t
case 117: // u
case 118: // v
case 119: // w
case 120: // x
case 121: // y
case 122:
// z
return readName(source, pos, _line, _col, prev);
}
throw syntaxError(source, pos, unexpectedCharacterMessage(code));
}
var line = lexer.line;
var col = 1 + pos - lexer.lineStart;
return new Token(TokenKind.EOF, bodyLength, bodyLength, line, col, prev);
}
/**
* Report a message that an unexpected character was encountered.
*/
function unexpectedCharacterMessage(code) {
if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) {
return "Cannot contain the invalid character ".concat(printCharCode(code), ".");
}
if (code === 39) {
// '
return 'Unexpected single quote character (\'), did you mean to use a double quote (")?';
}
return "Cannot parse the unexpected character ".concat(printCharCode(code), ".");
}
/**
* Reads a comment token from the source file.
*
* #[\u0009\u0020-\uFFFF]*
*/
function readComment(source, start, line, col, prev) {
var body = source.body;
var code;
var position = start;
do {
code = body.charCodeAt(++position);
} while (!isNaN(code) && ( // SourceCharacter but not LineTerminator
code > 0x001f || code === 0x0009));
return new Token(TokenKind.COMMENT, start, position, line, col, prev, body.slice(start + 1, position));
}
/**
* Reads a number token from the source file, either a float
* or an int depending on whether a decimal point appears.
*
* Int: -?(0|[1-9][0-9]*)
* Float: -?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(+|-)?[0-9]+)?
*/
function readNumber(source, start, firstCode, line, col, prev) {
var body = source.body;
var code = firstCode;
var position = start;
var isFloat = false;
if (code === 45) {
// -
code = body.charCodeAt(++position);
}
if (code === 48) {
// 0
code = body.charCodeAt(++position);
if (code >= 48 && code <= 57) {
throw syntaxError(source, position, "Invalid number, unexpected digit after 0: ".concat(printCharCode(code), "."));
}
} else {
position = readDigits(source, position, code);
code = body.charCodeAt(position);
}
if (code === 46) {
// .
isFloat = true;
code = body.charCodeAt(++position);
position = readDigits(source, position, code);
code = body.charCodeAt(position);
}
if (code === 69 || code === 101) {
// E e
isFloat = true;
code = body.charCodeAt(++position);
if (code === 43 || code === 45) {
// + -
code = body.charCodeAt(++position);
}
position = readDigits(source, position, code);
code = body.charCodeAt(position);
} // Numbers cannot be followed by . or NameStart
if (code === 46 || isNameStart(code)) {
throw syntaxError(source, position, "Invalid number, expected digit but got: ".concat(printCharCode(code), "."));
}
return new Token(isFloat ? TokenKind.FLOAT : TokenKind.INT, start, position, line, col, prev, body.slice(start, position));
}
/**
* Returns the new position in the source after reading digits.
*/
function readDigits(source, start, firstCode) {
var body = source.body;
var position = start;
var code = firstCode;
if (code >= 48 && code <= 57) {
// 0 - 9
do {
code = body.charCodeAt(++position);
} while (code >= 48 && code <= 57); // 0 - 9
return position;
}
throw syntaxError(source, position, "Invalid number, expected digit but got: ".concat(printCharCode(code), "."));
}
/**
* Reads a string token from the source file.
*
* "([^"\\\u000A\u000D]|(\\(u[0-9a-fA-F]{4}|["\\/bfnrt])))*"
*/
function readString(source, start, line, col, prev) {
var body = source.body;
var position = start + 1;
var chunkStart = position;
var code = 0;
var value = '';
while (position < body.length && !isNaN(code = body.charCodeAt(position)) && // not LineTerminator
code !== 0x000a && code !== 0x000d) {
// Closing Quote (")
if (code === 34) {
value += body.slice(chunkStart, position);
return new Token(TokenKind.STRING, start, position + 1, line, col, prev, value);
} // SourceCharacter
if (code < 0x0020 && code !== 0x0009) {
throw syntaxError(source, position, "Invalid character within String: ".concat(printCharCode(code), "."));
}
++position;
if (code === 92) {
// \
value += body.slice(chunkStart, position - 1);
code = body.charCodeAt(position);
switch (code) {
case 34:
value += '"';
break;
case 47:
value += '/';
break;
case 92:
value += '\\';
break;
case 98:
value += '\b';
break;
case 102:
value += '\f';
break;
case 110:
value += '\n';
break;
case 114:
value += '\r';
break;
case 116:
value += '\t';
break;
case 117:
{
// uXXXX
var charCode = uniCharCode(body.charCodeAt(position + 1), body.charCodeAt(position + 2), body.charCodeAt(position + 3), body.charCodeAt(position + 4));
if (charCode < 0) {
var invalidSequence = body.slice(position + 1, position + 5);
throw syntaxError(source, position, "Invalid character escape sequence: \\u".concat(invalidSequence, "."));
}
value += String.fromCharCode(charCode);
position += 4;
break;
}
default:
throw syntaxError(source, position, "Invalid character escape sequence: \\".concat(String.fromCharCode(code), "."));
}
++position;
chunkStart = position;
}
}
throw syntaxError(source, position, 'Unterminated string.');
}
/**
* Reads a block string token from the source file.
*
* """("?"?(\\"""|\\(?!=""")|[^"\\]))*"""
*/
function readBlockString(source, start, line, col, prev, lexer) {
var body = source.body;
var position = start + 3;
var chunkStart = position;
var code = 0;
var rawValue = '';
while (position < body.length && !isNaN(code = body.charCodeAt(position))) {
// Closing Triple-Quote (""")
if (code === 34 && body.charCodeAt(position + 1) === 34 && body.charCodeAt(position + 2) === 34) {
rawValue += body.slice(chunkStart, position);
return new Token(TokenKind.BLOCK_STRING, start, position + 3, line, col, prev, dedentBlockStringValue(rawValue));
} // SourceCharacter
if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) {
throw syntaxError(source, position, "Invalid character within String: ".concat(printCharCode(code), "."));
}
if (code === 10) {
// new line
++position;
++lexer.line;
lexer.lineStart = position;
} else if (code === 13) {
// carriage return
if (body.charCodeAt(position + 1) === 10) {
position += 2;
} else {
++position;
}
++lexer.line;
lexer.lineStart = position;
} else if ( // Escape Triple-Quote (\""")
code === 92 && body.charCodeAt(position + 1) === 34 && body.charCodeAt(position + 2) === 34 && body.charCodeAt(position + 3) === 34) {
rawValue += body.slice(chunkStart, position) + '"""';
position += 4;
chunkStart = position;
} else {
++position;
}
}
throw syntaxError(source, position, 'Unterminated string.');
}
/**
* Converts four hexadecimal chars to the integer that the
* string represents. For example, uniCharCode('0','0','0','f')
* will return 15, and uniCharCode('0','0','f','f') returns 255.
*
* Returns a negative number on error, if a char was invalid.
*
* This is implemented by noting that char2hex() returns -1 on error,
* which means the result of ORing the char2hex() will also be negative.
*/
function uniCharCode(a, b, c, d) {
return char2hex(a) << 12 | char2hex(b) << 8 | char2hex(c) << 4 | char2hex(d);
}
/**
* Converts a hex character to its integer value.
* '0' becomes 0, '9' becomes 9
* 'A' becomes 10, 'F' becomes 15
* 'a' becomes 10, 'f' becomes 15
*
* Returns -1 on error.
*/
function char2hex(a) {
return a >= 48 && a <= 57 ? a - 48 // 0-9
: a >= 65 && a <= 70 ? a - 55 // A-F
: a >= 97 && a <= 102 ? a - 87 // a-f
: -1;
}
/**
* Reads an alphanumeric + underscore name from the source.
*
* [_A-Za-z][_0-9A-Za-z]*
*/
function readName(source, start, line, col, prev) {
var body = source.body;
var bodyLength = body.length;
var position = start + 1;
var code = 0;
while (position !== bodyLength && !isNaN(code = body.charCodeAt(position)) && (code === 95 || // _
code >= 48 && code <= 57 || // 0-9
code >= 65 && code <= 90 || // A-Z
code >= 97 && code <= 122) // a-z
) {
++position;
}
return new Token(TokenKind.NAME, start, position, line, col, prev, body.slice(start, position));
} // _ A-Z a-z
function isNameStart(code) {
return code === 95 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
}
/**
* Configuration options to control parser behavior
*/
/**
* Given a GraphQL source, parses it into a Document.
* Throws GraphQLError if a syntax error is encountered.
*/
function parse(source, options) {
var parser = new Parser(source, options);
return parser.parseDocument();
}
/**
* This class is exported only to assist people in implementing their own parsers
* without duplicating too much code and should be used only as last resort for cases
* such as experimental syntax or if certain features could not be contributed upstream.
*
* It is still part of the internal API and is versioned, so any changes to it are never
* considered breaking changes. If you still need to support multiple versions of the
* library, please use the `versionInfo` variable for version detection.
*
* @internal
*/
var Parser = /*#__PURE__*/function () {
function Parser(source, options) {
var sourceObj = isSource(source) ? source : new Source(source);
this._lexer = new Lexer(sourceObj);
this._options = options;
}
/**
* Converts a name lex token into a name parse node.
*/
var _proto = Parser.prototype;
_proto.parseName = function parseName() {
var token = this.expectToken(TokenKind.NAME);
return {
kind: Kind.NAME,
value: