UNPKG

@theguild/federation-composition

Version:
61 lines (60 loc) • 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Contexts = void 0; class Contexts { bitByContext = new Map(); byTypeName = new Map(); constructor(supergraphState) { this.addUnionContexts(supergraphState); for (const type of supergraphState.objectTypes.values()) { this.addTypeContexts(supergraphState, type); } for (const type of supergraphState.interfaceTypes.values()) { this.addTypeContexts(supergraphState, type); } } maskOf(context) { return this.bitFor(context); } maskFor(contexts) { let mask = 0n; for (const context of contexts) { mask |= this.bitFor(context); } return mask; } bitFor(context) { let bit = this.bitByContext.get(context); if (bit === undefined) { bit = 1n << BigInt(this.bitByContext.size); this.bitByContext.set(context, bit); } return bit; } addMask(typeName, mask) { if (mask === 0n) { return; } const existing = this.byTypeName.get(typeName) ?? 0n; this.byTypeName.set(typeName, existing | mask); } addUnionContexts(supergraphState) { for (const union of supergraphState.unionTypes.values()) { const mask = this.maskFor(union.contexts); for (const member of union.members) { this.addMask(member, mask); } } } addTypeContexts(supergraphState, type) { let mask = this.maskFor(type.contexts); for (const interfaceName of type.interfaces) { const interfaceType = supergraphState.interfaceTypes.get(interfaceName); if (interfaceType) { mask |= this.maskFor(interfaceType.contexts); } } this.addMask(type.name, mask); } } exports.Contexts = Contexts;