Skip to content

Commit

Permalink
Make wrapped plans EXPORTABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Dec 9, 2023
1 parent 8a76db0 commit 54a99ea
Showing 1 changed file with 63 additions and 38 deletions.
101 changes: 63 additions & 38 deletions graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
FieldPlanResolver,
GrafastFieldConfig,
} from "grafast";
import { isExecutableStep } from "grafast";

type ToOptional<T> = { [K in keyof T]+?: T[K] };

Expand Down Expand Up @@ -97,7 +96,10 @@ export function makeWrapPlansPlugin<T>(
},
GraphQLObjectType_fields_field(field, build, context) {
const rules = (build as any)[symbol].rules as PlanWrapperRules | null;
const { access, ExecutableStep } = build.grafast;
const {
EXPORTABLE,
grafast: { access, ExecutableStep, isExecutableStep },
} = build;
const filter = (build as any)[symbol]
.filter as PlanWrapperFilter<T> | null;
const {
Expand Down Expand Up @@ -146,44 +148,67 @@ export function makeWrapPlansPlugin<T>(
} = field;
return {
...field,
plan(...planParams) {
// A replacement for `oldPlan` that automatically passes through arguments that weren't replaced
const smartPlan = (...overrideParams: Array<any>) => {
const $prev = oldPlan(
// @ts-ignore We're calling it dynamically, allowing the parent to override args.
...overrideParams.concat(
planParams.slice(overrideParams.length),
),
);
if (!($prev instanceof ExecutableStep)) {
console.error(
`Wrapped a plan function, but that function did not return a step!\n${String(
oldPlan,
)}\n${inspect(field)}`,
);
plan: EXPORTABLE(
(
ExecutableStep,
field,
inspect,
isExecutableStep,
oldPlan,
planWrapper,
) =>
(...planParams) => {
// A replacement for `oldPlan` that automatically passes through arguments that weren't replaced
const smartPlan = (...overrideParams: Array<any>) => {
const $prev = oldPlan(
// @ts-ignore We're calling it dynamically, allowing the parent to override args.
...overrideParams.concat(
planParams.slice(overrideParams.length),
),
);
if (!($prev instanceof ExecutableStep)) {
console.error(
`Wrapped a plan function, but that function did not return a step!\n${String(
oldPlan,
)}\n${inspect(field)}`,
);

throw new Error(
"Wrapped a plan function, but that function did not return a step!",
throw new Error(
"Wrapped a plan function, but that function did not return a step!",
);
}
return $prev;
};
const [$source, fieldArgs, info] = planParams;
const $newPlan = planWrapper(
smartPlan,
$source,
fieldArgs,
info,
);
}
return $prev;
};
const [$source, fieldArgs, info] = planParams;
const $newPlan = planWrapper(smartPlan, $source, fieldArgs, info);
if ($newPlan === undefined) {
throw new Error(
"Your plan wrapper didn't return anything; it must return a step or null!",
);
}
if ($newPlan !== null && !isExecutableStep($newPlan)) {
throw new Error(
`Your plan wrapper returned something other than a step... It must return a step (or null). (Returned: ${inspect(
$newPlan,
)})`,
);
}
return $newPlan;
},
if ($newPlan === undefined) {
throw new Error(
"Your plan wrapper didn't return anything; it must return a step or null!",
);
}
if ($newPlan !== null && !isExecutableStep($newPlan)) {
throw new Error(
`Your plan wrapper returned something other than a step... It must return a step (or null). (Returned: ${inspect(
$newPlan,
)})`,
);
}
return $newPlan;
},
[
ExecutableStep,
field,
inspect,
isExecutableStep,
oldPlan,
planWrapper,
],
),
};
},
},
Expand Down

0 comments on commit 54a99ea

Please sign in to comment.