@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
74 lines (73 loc) • 2.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperationPath = exports.emptyOverrideLabel = void 0;
const edge_js_1 = require("./edge.js");
const helpers_js_1 = require("./helpers.js");
exports.emptyOverrideLabel = " ";
class OperationPath {
_rootNode;
_toString = (0, helpers_js_1.lazy)(() => {
let str = this._rootNode.toString();
for (let i = 0; i < this.previousEdges.length; i++) {
const edge = this.previousEdges[i];
if (edge) {
str += ` -(${edge.move})-> ${edge.tail}`;
}
}
return str;
});
previousEdges = [];
contextsInScope = 0n;
get contexts() {
return this.contextsInScope;
}
constructor(_rootNode) {
this._rootNode = _rootNode;
}
move(edge) {
this._toString.invalidate();
this.previousEdges.push(edge);
if ((0, edge_js_1.isFieldEdge)(edge) && edge.head.contexts !== 0n) {
this.contextsInScope |= edge.head.contexts;
}
return this;
}
clone() {
const newPath = new OperationPath(this._rootNode);
newPath.previousEdges = this.previousEdges.slice();
newPath.contextsInScope = this.contextsInScope;
return newPath;
}
withoutContexts() {
const newPath = this.clone();
newPath.contextsInScope = 0n;
return newPath;
}
hasContexts(required) {
return (this.contextsInScope & required) === required;
}
depth() {
return this.previousEdges.length;
}
edge() {
return this.previousEdges[this.previousEdges.length - 1];
}
steps() {
return this.previousEdges.map((edge) => (0, edge_js_1.isFieldEdge)(edge)
? { typeName: edge.move.typeName, fieldName: edge.move.fieldName }
: { typeName: edge.tail.typeName });
}
tail() {
return this.edge()?.tail;
}
rootNode() {
return this._rootNode;
}
isVisitedEdge(edge) {
return this.previousEdges.includes(edge);
}
toString() {
return this._toString.get();
}
}
exports.OperationPath = OperationPath;