Skip to content

Commit

Permalink
Middleware around establishing an operation plan
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed May 20, 2024
1 parent fb68414 commit 70d42cf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
2 changes: 2 additions & 0 deletions grafast/grafast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import type {
$$queryCache,
CacheByOperationEntry,
DataFromStep,
EstablishOperationPlanEvent,
ExecuteEvent,
GrafastExecutionArgs,
GrafastTimeouts,
Expand Down Expand Up @@ -756,6 +757,7 @@ declare global {
): PromiseOrDirect<Grafast.ExecutionArgs>;
execute(event: ExecuteEvent): ReturnType<typeof execute>;
subscribe(event: ExecuteEvent): ReturnType<typeof subscribe>;
establishOperationPlan(event: EstablishOperationPlanEvent): OperationPlan;
}
interface Plugin {
grafast?: {
Expand Down
12 changes: 12 additions & 0 deletions grafast/grafast/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import type {
GraphQLScalarType,
GraphQLSchema,
GraphQLType,
OperationDefinitionNode,
Source,
ValueNode,
VariableNode,
} from "graphql";
import type { ObjMap } from "graphql/jsutils/ObjMap.js";

import type { Bucket, RequestTools } from "./bucket.js";
import type { OperationPlan } from "./engine/OperationPlan.js";
Expand Down Expand Up @@ -972,3 +974,13 @@ export interface ExecuteEvent {
export interface SubscribeEvent {
args: GrafastExecutionArgs;
}
export interface EstablishOperationPlanEvent {
schema: GraphQLSchema;
operation: OperationDefinitionNode;
fragments: ObjMap<FragmentDefinitionNode>;
variableValues: Record<string, any>;
context: any;
rootValue: any;
planningTimeout: number | undefined;
args: GrafastExecutionArgs;
}
57 changes: 46 additions & 11 deletions grafast/grafast/src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ import { POLYMORPHIC_ROOT_PATH } from "./engine/OperationPlan.js";
import type { OutputPlan } from "./engine/OutputPlan.js";
import { coerceError, getChildBucketAndIndex } from "./engine/OutputPlan.js";
import { establishOperationPlan } from "./establishOperationPlan.js";
import type { GrafastPlanJSON, OperationPlan } from "./index.js";
import type {
GrafastExecutionArgs,
GrafastPlanJSON,
OperationPlan,
} from "./index.js";
import type {
EstablishOperationPlanEvent,
GrafastTimeouts,
JSONValue,
PromiseOrDirect,
Expand Down Expand Up @@ -537,11 +542,23 @@ declare module "./engine/OperationPlan.js" {
}
}

function establishOperationPlanFromEvent(event: EstablishOperationPlanEvent) {
return establishOperationPlan(
event.schema,
event.operation,
event.fragments,
event.variableValues,
event.context as any,
event.rootValue,
event.planningTimeout,
);
}

/**
* @internal
*/
export function grafastPrepare(
args: ExecutionArgs,
args: GrafastExecutionArgs,
options: GrafastPrepareOptions = {},
): PromiseOrDirect<
ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, void>
Expand All @@ -552,6 +569,7 @@ export function grafastPrepare(
rootValue = Object.create(null),
// operationName,
// document,
middlewares,
} = args;
const exeContext = buildExecutionContext(args);

Expand All @@ -567,15 +585,32 @@ export function grafastPrepare(
const planningTimeout = options.timeouts?.planning;
let operationPlan!: OperationPlan;
try {
operationPlan = establishOperationPlan(
schema,
operation,
fragments,
variableValues,
context as any,
rootValue,
planningTimeout,
);
if (middlewares) {
operationPlan = middlewares.run(
"establishOperationPlan",
{
schema,
operation,
fragments,
variableValues,
context: context as any,
rootValue,
planningTimeout,
args,
},
establishOperationPlanFromEvent,
);
} else {
operationPlan = establishOperationPlan(
schema,
operation,
fragments,
variableValues,
context as any,
rootValue,
planningTimeout,
);
}
} catch (error) {
const graphqlError =
error instanceof GraphQLError
Expand Down

0 comments on commit 70d42cf

Please sign in to comment.