grafast
Version:
Cutting edge GraphQL planning and execution engine
87 lines • 3.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoadedRecordStep = void 0;
const step_js_1 = require("../step.js");
const utils_js_1 = require("../utils.js");
const access_js_1 = require("./access.js");
const constant_js_1 = require("./constant.js");
const loadMany_js_1 = require("./loadMany.js");
/**
* You shouldn't create instances of this yourself - use `loadOne` or `loadMany` instead.
*/
class LoadedRecordStep extends step_js_1.Step {
static { this.$$export = {
moduleName: "grafast",
exportName: "LoadedRecordStep",
}; }
constructor($loadMany, $data, isSingle, sourceDescription,
// Only safe to reference this during planning phase
ioEquivalence) {
super();
this.isSingle = isSingle;
this.sourceDescription = sourceDescription;
this.ioEquivalence = ioEquivalence;
this.isSyncAndSafe = true;
this.attributes = new Set();
this.paramDepIdByKey = Object.create(null);
this.addDependency($data);
this.addDependency($loadMany);
}
toStringMeta() {
return this.sourceDescription ?? null;
}
get(attr) {
return this.cacheStep("get", attr, () => this._getInner(attr));
}
_getInner(attr) {
// Allow auto-collapsing of the waterfall by knowing keys are equivalent
if (this.operationPlan.phase === "plan" &&
this.ioEquivalence[attr]) {
return this.ioEquivalence[attr];
}
this.attributes.add(attr);
return (0, access_js_1.access)(this, attr);
}
setParam(paramKey, value) {
if (!this.isSingle) {
throw new Error("setParam should not be called on list items - call it on the collection (`loadMany()` step)");
}
this.paramDepIdByKey[paramKey] = this.addUnaryDependency(value instanceof step_js_1.Step ? value : (0, constant_js_1.constant)(value));
}
deduplicate(peers) {
return peers.filter((p) => p.isSingle === this.isSingle &&
p.sourceDescription === this.sourceDescription &&
(0, utils_js_1.recordsMatch)(p.ioEquivalence, this.ioEquivalence) &&
(0, utils_js_1.recordsMatch)(p.paramDepIdByKey, this.paramDepIdByKey));
}
deduplicatedWith(replacement) {
for (const attr of this.attributes) {
replacement.attributes.add(attr);
}
}
optimize() {
const $source = this.getDepDeep(1);
if ($source instanceof loadMany_js_1.LoadManyStep) {
// Tell our parent we only need certain attributes
$source.addAttributes(this.attributes);
for (const [key, depId] of Object.entries(this.paramDepIdByKey)) {
$source.setParam(key, this.getDep(depId));
}
}
else {
// This should never happen
throw new Error(`LoadedRecordStep could not find the parent LoadManyStep; instead found ${$source}`);
}
// Record has no run-time behaviour (it's just a plan-time helper), so we
// can replace ourself with our dependency:
return this.getDep(0);
}
// This'll never be called, due to `optimize` above.
execute({ count, values: [values0], }) {
return values0.isBatch
? values0.entries
: (0, utils_js_1.arrayOfLength)(count, values0.value);
}
}
exports.LoadedRecordStep = LoadedRecordStep;
//# sourceMappingURL=loadedRecord.js.map