UNPKG

grafast

Version:

Cutting edge GraphQL planning and execution engine

99 lines 4.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withGlobalLayerPlan = withGlobalLayerPlan; exports.currentLayerPlan = currentLayerPlan; exports.currentPolymorphicPaths = currentPolymorphicPaths; exports.currentPlanningPath = currentPlanningPath; exports.currentFieldStreamDetails = currentFieldStreamDetails; exports.isUnaryStep = isUnaryStep; const global_js_1 = require("../../global.js"); const constant_1 = require("../../steps/constant"); const lambda_js_1 = require("../../steps/lambda.js"); const object_js_1 = require("../../steps/object.js"); let globalData_layerPlan = undefined; let globalData_polymorphicPaths = undefined; let globalData_planningPath = undefined; let globalData_stepStreamOptions = undefined; function withGlobalLayerPlan(layerPlan, polymorphicPaths, planningPath, stepStreamOptions, callback, callbackThis, ...callbackArgs) { const oldLayerPlan = globalData_layerPlan; globalData_layerPlan = layerPlan; const oldPolymorphicPaths = globalData_polymorphicPaths; globalData_polymorphicPaths = polymorphicPaths; const oldStepStreamOptions = globalData_stepStreamOptions; globalData_stepStreamOptions = stepStreamOptions; const oldPlanningPath = globalData_planningPath; // Keep the old planning path if we've not been given a new one if (planningPath != null) { globalData_planningPath = planningPath; } try { return callback.apply(callbackThis, callbackArgs); } finally { globalData_layerPlan = oldLayerPlan; globalData_polymorphicPaths = oldPolymorphicPaths; globalData_stepStreamOptions = oldStepStreamOptions; globalData_planningPath = oldPlanningPath; } } function currentLayerPlan() { if (globalData_layerPlan === undefined) { throw new Error( // Must only be called from inside `withGlobalLayerPlan`! "Now is not a valid time to call `currentLayerPlan`. This error typically occurs when you attempt to call a Grafast step function from outside of the planning lifecycle - it's important to note that Grafast plans must be resolved synchronously, so check for 'async' or 'setTimeout' or any location where a step function is called outside of a plan resolver. For more information, read about plan resolvers: https://grafast.org/grafast/plan-resolvers"); } return globalData_layerPlan; } function currentPolymorphicPaths() { if (globalData_polymorphicPaths === undefined) { throw new Error("GrafastInternalError<b0b05743-8b21-42c6-9b53-925013d88bd1>: currentPolymorphicPaths called out of turn; must only called within a withGlobalLayerPlan callback"); } return globalData_polymorphicPaths; } /** UNUSED */ function currentPlanningPath() { const opPlan = (0, global_js_1.operationPlan)(); if (opPlan.phase === "plan" && globalData_planningPath === undefined) { // This might happen during deduplicate for example console.warn("GrafastInternalWarning<6a484dc3-a690-493c-b8b3-d6196bf2c290>: currentPlanningPath could not retrieve the current path even in 'plan' phase"); } return globalData_planningPath; } /** * Step - stream details. * true - Not a stream, but it is a subscription. * null - neither stream nor subscription * * @experimental */ function currentFieldStreamDetails() { if (globalData_stepStreamOptions == null) { return null; } if (globalData_stepStreamOptions === true) { return true; } const { ifStepId, initialCountStepId } = globalData_stepStreamOptions; return (0, global_js_1.operationPlan)().withRootLayerPlan(() => { const $obj = (0, object_js_1.object)({ if: ifStepId ? (0, global_js_1.operationPlan)().dangerouslyGetStep(ifStepId) : (0, constant_1.constant)(undefined), initialCount: initialCountStepId ? (0, global_js_1.operationPlan)().dangerouslyGetStep(initialCountStepId) : (0, constant_1.constant)(undefined), }); return (0, lambda_js_1.lambda)($obj, resolveStreamDetails, true); }); } function resolveStreamDetails(details) { if (details.if === false) return null; return { initialCount: details.initialCount ?? 0, }; } function isUnaryStep($step) { return $step._isUnary; } //# sourceMappingURL=withGlobalLayerPlan.js.map