Skip to content

Commit

Permalink
Middleware down into step execution
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed May 20, 2024
1 parent 70d42cf commit cd993e3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
4 changes: 3 additions & 1 deletion grafast/grafast/src/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import type { GraphQLScalarType } from "graphql";

import type { ExecutableStep } from ".";
import type { ExecutableStep, GrafastExecutionArgs } from ".";
import type { LayerPlan } from "./engine/LayerPlan";
import type { MetaByMetaKey } from "./engine/OperationPlan";
import type {
Expand All @@ -13,6 +13,8 @@ import type {
* @internal
*/
export interface RequestTools {
/** @internal */
args: GrafastExecutionArgs;
/** The `timeSource.now()` at which the request started executing */
startTime: number;
/** The `timeSource.now()` at which the request should stop executing (if a timeout was configured) */
Expand Down
42 changes: 37 additions & 5 deletions grafast/grafast/src/engine/executeBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ import { isFlaggedValue, SafeError } from "../error.js";
import { inspect } from "../inspect.js";
import type {
BatchExecutionValue,
ExecuteStepEvent,
ExecutionDetails,
ExecutionEntryFlags,
ExecutionExtra,
ExecutionValue,
ForcedValues,
GrafastExecutionArgs,
GrafastInternalResultsOrStream,
GrafastResultsList,
GrafastResultStreamList,
IndexForEach,
IndexMap,
PromiseOrDirect,
StreamDetails,
StreamMaybeMoreableArray,
StreamStepEvent,
UnaryExecutionValue,
UnbatchedExecutionExtra,
} from "../interfaces.js";
Expand Down Expand Up @@ -111,7 +116,8 @@ export function executeBucket(
}
}

const { stopTime, eventEmitter } = requestContext;
const { stopTime, eventEmitter, args } = requestContext;
const { middlewares } = args;
const {
metaByMetaKey,
size,
Expand Down Expand Up @@ -750,27 +756,45 @@ export function executeBucket(
`${step} is using a legacy form of 'stream' which accepts multiple arguments, please see https://err.red/gev2`,
);
}
return step.stream({
const streamDetails: StreamDetails<readonly any[]> = {
indexMap: makeIndexMap(count),
indexForEach: makeIndexForEach(count),
count,
values,
extra,
streamOptions,
});
};
if (!step.isSyncAndSafe && middlewares) {
return middlewares.run(
"streamStep",
{ args, step, streamDetails },
streamStepFromEvent,
);
} else {
return step.stream(streamDetails);
}
} else {
if (step.execute.length > 1) {
throw new Error(
`${step} is using a legacy form of 'execute' which accepts multiple arguments, please see https://err.red/gev2`,
);
}
return step.execute({
const executeDetails: ExecutionDetails<readonly any[]> = {
indexMap: makeIndexMap(count),
indexForEach: makeIndexForEach(count),
count,
values,
extra,
});
};
if (!step.isSyncAndSafe && middlewares) {
return middlewares.run(
"executeStep",
{ args, step, executeDetails },
executeStepFromEvent,
);
} else {
return step.execute(executeDetails);
}
}
}

Expand Down Expand Up @@ -1406,3 +1430,11 @@ function makeIndexForEach(count: number) {
}
return result;
}

function streamStepFromEvent(event: StreamStepEvent) {
return event.step.stream(event.streamDetails);
}

function executeStepFromEvent(event: ExecuteStepEvent) {
return event.step.execute(event.executeDetails);
}
8 changes: 8 additions & 0 deletions grafast/grafast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ import type {
DataFromStep,
EstablishOperationPlanEvent,
ExecuteEvent,
ExecuteStepEvent,
GrafastExecutionArgs,
GrafastTimeouts,
ParseAndValidateEvent,
PrepareArgsEvent,
ScalarInputPlanResolver,
StreamStepEvent,
ValidateSchemaEvent,
} from "./interfaces.js";
import {
Expand Down Expand Up @@ -758,6 +760,12 @@ declare global {
execute(event: ExecuteEvent): ReturnType<typeof execute>;
subscribe(event: ExecuteEvent): ReturnType<typeof subscribe>;
establishOperationPlan(event: EstablishOperationPlanEvent): OperationPlan;
executeStep(
event: ExecuteStepEvent,
): PromiseOrDirect<GrafastResultsList<any>>;
streamStep(
event: StreamStepEvent,
): PromiseOrDirect<GrafastResultStreamList<unknown>>;
}
interface Plugin {
grafast?: {
Expand Down
17 changes: 16 additions & 1 deletion grafast/grafast/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import type { ObjMap } from "graphql/jsutils/ObjMap.js";
import type { Bucket, RequestTools } from "./bucket.js";
import type { OperationPlan } from "./engine/OperationPlan.js";
import type { FlaggedValue, SafeError } from "./error.js";
import type { ExecutableStep, ListCapableStep, ModifierStep } from "./step.js";
import type {
ExecutableStep,
ListCapableStep,
ModifierStep,
StreamableStep,
} from "./step.js";
import type { __InputDynamicScalarStep } from "./steps/__inputDynamicScalar.js";
import type {
__InputListStep,
Expand Down Expand Up @@ -984,3 +989,13 @@ export interface EstablishOperationPlanEvent {
planningTimeout: number | undefined;
args: GrafastExecutionArgs;
}
export interface ExecuteStepEvent {
args: GrafastExecutionArgs;
step: ExecutableStep;
executeDetails: ExecutionDetails;
}
export interface StreamStepEvent {
args: GrafastExecutionArgs;
step: StreamableStep<unknown>;
streamDetails: StreamDetails;
}
3 changes: 3 additions & 0 deletions grafast/grafast/src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ function outputBucket(
}

function executePreemptive(
args: GrafastExecutionArgs,
operationPlan: OperationPlan,
variableValues: any,
context: any,
Expand Down Expand Up @@ -337,6 +338,7 @@ function executePreemptive(
const stopTime =
executionTimeout !== null ? startTime + executionTimeout : null;
const requestContext: RequestTools = {
args,
startTime,
stopTime,
// toSerialize: [],
Expand Down Expand Up @@ -644,6 +646,7 @@ export function grafastPrepare(

const executionTimeout = options.timeouts?.execution ?? null;
return executePreemptive(
args,
operationPlan,
variableValues,
context,
Expand Down

0 comments on commit cd993e3

Please sign in to comment.