UNPKG

grafast

Version:

Cutting edge GraphQL planning and execution engine

77 lines 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FirstStep = void 0; exports.first = first; const constants_ts_1 = require("../constants.js"); const step_ts_1 = require("../step.js"); const connection_ts_1 = require("./connection.js"); const list_ts_1 = require("./list.js"); function unbatchedExecute(_extra, list) { return list?.[0]; } function optimalExecute({ indexMap, values: [values0], }) { return indexMap((i) => values0.at(i)?.[0]); } class FirstStep extends step_ts_1.Step { static { this.$$export = { moduleName: "grafast", exportName: "FirstStep", }; } constructor(parentPlan, isArray = true) { super(); this.allowMultipleOptimizations = true; this.addStrongDependency((0, connection_ts_1.itemsOrStep)(parentPlan)); if (isArray) { this.unbatchedExecute = unbatchedExecute; this.execute = (optimalExecute); this.isSyncAndSafe = true; } else { this.isSyncAndSafe = false; } } [constants_ts_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[0]; // Iterable? Return the first entry return (async () => { for await (const e of val) { return e; } return undefined; })(); }); } deduplicate(peers) { return peers; } optimize() { const parent = this.getDep(0); // The first of a list plan is just the first dependency of the list plan. if (parent instanceof list_ts_1.ListStep) { return parent.first(); } return this; } } exports.FirstStep = FirstStep; /** * A plan that resolves to the first 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 first(plan, array = true) { return plan.operationPlan.cacheStep(plan, "GrafastInternal:first()", array, () => new FirstStep(plan, array)); } //# sourceMappingURL=first.js.map