UNPKG

@theguild/federation-composition

Version:
70 lines (69 loc) • 1.97 kB
import { isFieldEdge } from "./edge.js"; import { lazy } from "./helpers.js"; export const emptyOverrideLabel = " "; export class OperationPath { _rootNode; _toString = 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 (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) => 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(); } }