grafast
Version:
Cutting edge GraphQL planning and execution engine
81 lines • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LastStep = void 0;
exports.last = last;
const constants_js_1 = require("../constants.js");
const step_js_1 = require("../step.js");
const connection_js_1 = require("./connection.js");
const list_js_1 = require("./list.js");
function unbatchedExecute(_extra, list) {
return list?.[list.length - 1];
}
function optimalExecute({ indexMap, values: [values0], }) {
return indexMap((i) => {
const list = values0.at(i);
return list?.[list.length - 1];
});
}
class LastStep extends step_js_1.Step {
static { this.$$export = {
moduleName: "grafast",
exportName: "LastStep",
}; }
constructor(parentPlan, isArray = true) {
super();
this.allowMultipleOptimizations = true;
this.addDependency((0, connection_js_1.itemsOrStep)(parentPlan));
if (isArray) {
this.unbatchedExecute = unbatchedExecute;
this.execute = (optimalExecute);
this.isSyncAndSafe = true;
}
else {
this.isSyncAndSafe = false;
}
}
[constants_js_1.$$deepDepSkip]() {
return this.getDepOptions(0).step;
}
execute({ indexMap, values: [values0], }) {
return indexMap((i) => {
const val = values0.at(i);
if (val == null)
return val;
if (Array.isArray(val))
return val[val.length - 1];
// Iterable? Return the last entry
return (async () => {
let last = undefined;
for await (const e of val) {
last = e;
}
return last;
})();
});
}
deduplicate(peers) {
return peers;
}
optimize() {
const parent = this.getDep(0);
// The last of a list plan is just the last dependency of the list plan.
if (parent instanceof list_js_1.ListStep) {
return parent.last();
}
return this;
}
}
exports.LastStep = LastStep;
/**
* A plan that resolves to the last entry in the list returned by the given
* plan.
*
* @param plan - the list plan
* @param array - set this true if the plan represents an array (or
* null/undefined) - i.e. it' won't be an (async) iterable - to enable greater
* optimization
*/
function last(plan, array = true) {
return plan.operationPlan.cacheStep(plan, "GrafastInternal:last()", array, () => new LastStep(plan, array));
}
//# sourceMappingURL=last.js.map