123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.visit = visit;
- exports.visitInParallel = visitInParallel;
- exports.visitWithTypeInfo = visitWithTypeInfo;
- exports.getVisitFn = getVisitFn;
- exports.BREAK = exports.QueryDocumentKeys = void 0;
- var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- var QueryDocumentKeys = {
- Name: [],
- Document: ['definitions'],
- OperationDefinition: ['name', 'variableDefinitions', 'directives', 'selectionSet'],
- VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'],
- Variable: ['name'],
- SelectionSet: ['selections'],
- Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
- Argument: ['name', 'value'],
- FragmentSpread: ['name', 'directives'],
- InlineFragment: ['typeCondition', 'directives', 'selectionSet'],
- FragmentDefinition: ['name',
-
- 'variableDefinitions', 'typeCondition', 'directives', 'selectionSet'],
- IntValue: [],
- FloatValue: [],
- StringValue: [],
- BooleanValue: [],
- NullValue: [],
- EnumValue: [],
- ListValue: ['values'],
- ObjectValue: ['fields'],
- ObjectField: ['name', 'value'],
- Directive: ['name', 'arguments'],
- NamedType: ['name'],
- ListType: ['type'],
- NonNullType: ['type'],
- SchemaDefinition: ['directives', 'operationTypes'],
- OperationTypeDefinition: ['type'],
- ScalarTypeDefinition: ['description', 'name', 'directives'],
- ObjectTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'],
- FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'],
- InputValueDefinition: ['description', 'name', 'type', 'defaultValue', 'directives'],
- InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'],
- UnionTypeDefinition: ['description', 'name', 'directives', 'types'],
- EnumTypeDefinition: ['description', 'name', 'directives', 'values'],
- EnumValueDefinition: ['description', 'name', 'directives'],
- InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
- DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
- SchemaExtension: ['directives', 'operationTypes'],
- ScalarTypeExtension: ['name', 'directives'],
- ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
- InterfaceTypeExtension: ['name', 'directives', 'fields'],
- UnionTypeExtension: ['name', 'directives', 'types'],
- EnumTypeExtension: ['name', 'directives', 'values'],
- InputObjectTypeExtension: ['name', 'directives', 'fields']
- };
- exports.QueryDocumentKeys = QueryDocumentKeys;
- var BREAK = Object.freeze({});
- exports.BREAK = BREAK;
- function visit(root, visitor) {
- var visitorKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : QueryDocumentKeys;
-
- var stack = undefined;
- var inArray = Array.isArray(root);
- var keys = [root];
- var index = -1;
- var edits = [];
- var node = undefined;
- var key = undefined;
- var parent = undefined;
- var path = [];
- var ancestors = [];
- var newRoot = root;
-
- do {
- index++;
- var isLeaving = index === keys.length;
- var isEdited = isLeaving && edits.length !== 0;
- if (isLeaving) {
- key = ancestors.length === 0 ? undefined : path[path.length - 1];
- node = parent;
- parent = ancestors.pop();
- if (isEdited) {
- if (inArray) {
- node = node.slice();
- } else {
- var clone = {};
- for (var _i2 = 0, _Object$keys2 = Object.keys(node); _i2 < _Object$keys2.length; _i2++) {
- var k = _Object$keys2[_i2];
- clone[k] = node[k];
- }
- node = clone;
- }
- var editOffset = 0;
- for (var ii = 0; ii < edits.length; ii++) {
- var editKey = edits[ii][0];
- var editValue = edits[ii][1];
- if (inArray) {
- editKey -= editOffset;
- }
- if (inArray && editValue === null) {
- node.splice(editKey, 1);
- editOffset++;
- } else {
- node[editKey] = editValue;
- }
- }
- }
- index = stack.index;
- keys = stack.keys;
- edits = stack.edits;
- inArray = stack.inArray;
- stack = stack.prev;
- } else {
- key = parent ? inArray ? index : keys[index] : undefined;
- node = parent ? parent[key] : newRoot;
- if (node === null || node === undefined) {
- continue;
- }
- if (parent) {
- path.push(key);
- }
- }
- var result = void 0;
- if (!Array.isArray(node)) {
- if (!isNode(node)) {
- throw new Error('Invalid AST Node: ' + (0, _inspect.default)(node));
- }
- var visitFn = getVisitFn(visitor, node.kind, isLeaving);
- if (visitFn) {
- result = visitFn.call(visitor, node, key, parent, path, ancestors);
- if (result === BREAK) {
- break;
- }
- if (result === false) {
- if (!isLeaving) {
- path.pop();
- continue;
- }
- } else if (result !== undefined) {
- edits.push([key, result]);
- if (!isLeaving) {
- if (isNode(result)) {
- node = result;
- } else {
- path.pop();
- continue;
- }
- }
- }
- }
- }
- if (result === undefined && isEdited) {
- edits.push([key, node]);
- }
- if (isLeaving) {
- path.pop();
- } else {
- stack = {
- inArray: inArray,
- index: index,
- keys: keys,
- edits: edits,
- prev: stack
- };
- inArray = Array.isArray(node);
- keys = inArray ? node : visitorKeys[node.kind] || [];
- index = -1;
- edits = [];
- if (parent) {
- ancestors.push(parent);
- }
- parent = node;
- }
- } while (stack !== undefined);
- if (edits.length !== 0) {
- newRoot = edits[edits.length - 1][1];
- }
- return newRoot;
- }
- function isNode(maybeNode) {
- return Boolean(maybeNode && typeof maybeNode.kind === 'string');
- }
- function visitInParallel(visitors) {
- var skipping = new Array(visitors.length);
- return {
- enter: function enter(node) {
- for (var i = 0; i < visitors.length; i++) {
- if (!skipping[i]) {
- var fn = getVisitFn(visitors[i], node.kind,
-
- false);
- if (fn) {
- var result = fn.apply(visitors[i], arguments);
- if (result === false) {
- skipping[i] = node;
- } else if (result === BREAK) {
- skipping[i] = BREAK;
- } else if (result !== undefined) {
- return result;
- }
- }
- }
- }
- },
- leave: function leave(node) {
- for (var i = 0; i < visitors.length; i++) {
- if (!skipping[i]) {
- var fn = getVisitFn(visitors[i], node.kind,
-
- true);
- if (fn) {
- var result = fn.apply(visitors[i], arguments);
- if (result === BREAK) {
- skipping[i] = BREAK;
- } else if (result !== undefined && result !== false) {
- return result;
- }
- }
- } else if (skipping[i] === node) {
- skipping[i] = null;
- }
- }
- }
- };
- }
- function visitWithTypeInfo(typeInfo, visitor) {
- return {
- enter: function enter(node) {
- typeInfo.enter(node);
- var fn = getVisitFn(visitor, node.kind,
-
- false);
- if (fn) {
- var result = fn.apply(visitor, arguments);
- if (result !== undefined) {
- typeInfo.leave(node);
- if (isNode(result)) {
- typeInfo.enter(result);
- }
- }
- return result;
- }
- },
- leave: function leave(node) {
- var fn = getVisitFn(visitor, node.kind,
-
- true);
- var result;
- if (fn) {
- result = fn.apply(visitor, arguments);
- }
- typeInfo.leave(node);
- return result;
- }
- };
- }
- function getVisitFn(visitor, kind, isLeaving) {
- var kindVisitor = visitor[kind];
- if (kindVisitor) {
- if (!isLeaving && typeof kindVisitor === 'function') {
-
- return kindVisitor;
- }
- var kindSpecificVisitor = isLeaving ? kindVisitor.leave : kindVisitor.enter;
- if (typeof kindSpecificVisitor === 'function') {
-
- return kindSpecificVisitor;
- }
- } else {
- var specificVisitor = isLeaving ? visitor.leave : visitor.enter;
- if (specificVisitor) {
- if (typeof specificVisitor === 'function') {
-
- return specificVisitor;
- }
- var specificKindVisitor = specificVisitor[kind];
- if (typeof specificKindVisitor === 'function') {
-
- return specificKindVisitor;
- }
- }
- }
- }
|