@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
57 lines (56 loc) • 1.72 kB
JavaScript
import { lazy } from './helpers.js';
import { AbstractMove, EntityMove, FieldMove } from './moves.js';
export function isEntityEdge(edge) {
return edge.move instanceof EntityMove;
}
export function assertEntityEdge(edge) {
if (!isEntityEdge(edge)) {
throw new Error(`Expected edge to be Edge<EntityMove>, but got ${edge}`);
}
}
export function isAbstractEdge(edge) {
return edge.move instanceof AbstractMove;
}
export function assertAbstractEdge(edge) {
if (!isAbstractEdge(edge)) {
throw new Error(`Expected edge to be Edge<AbstractMove>, but got ${edge}`);
}
}
export function isFieldEdge(edge) {
return edge.move instanceof FieldMove;
}
export function assertFieldEdge(edge) {
if (!isFieldEdge(edge)) {
throw new Error(`Expected edge to be Edge<FieldMove>, but got ${edge}`);
}
}
export class Edge {
head;
move;
tail;
resolvable = [];
_toString = lazy(() => `${this.head} -(${this.move})-> ${this.tail}`);
constructor(head, move, tail) {
this.head = head;
this.move = move;
this.tail = tail;
}
isCrossGraphEdge() {
return this.head.graphId !== this.tail.graphId;
}
toString() {
return this._toString.get();
}
getResolvability(graphNames) {
return this.resolvable.find(([checkedGraphNames]) => {
return checkedGraphNames.every(name => {
return graphNames.includes(name);
});
})?.[1];
}
setResolvable(success, graphNames, error) {
const result = success ? { success, error: undefined } : { success, error: error };
this.resolvable.push([graphNames, result]);
return result;
}
}