Skip to content

Commit

Permalink
Add __assertStep support to makeExtendSchemaPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Jan 19, 2024
1 parent d8d2803 commit 1dd9c30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 7 additions & 1 deletion grafast/grafast/src/makeGrafastSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export type FieldPlans =
* The plans/config for each field of a GraphQL object type.
*/
export type ObjectPlans = {
__Step?: { new (...args: any[]): ExecutableStep };
__assertStep?:
| ((step: ExecutableStep) => asserts step is ExecutableStep)
| { new (...args: any[]): ExecutableStep };
} & {
[fieldName: string]: FieldPlans;
};
Expand Down Expand Up @@ -142,6 +144,10 @@ export function makeGrafastSchema(details: {
type.extensions as graphql.GraphQLObjectTypeExtensions<any, any>
).grafast = { assertStep: fieldSpec as any };
continue;
} else if (fieldName.startsWith("__")) {
throw new Error(
`Unsupported field name '${fieldName}'; perhaps you meant '__assertStep'?`,
);
}

const field = fields[fieldName];
Expand Down
20 changes: 17 additions & 3 deletions graphile-build/graphile-utils/src/makeExtendSchemaPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FieldPlanResolver } from "grafast";
import type { ExecutableStep, FieldPlanResolver } from "grafast";
import type {
DefinitionNode,
DirectiveDefinitionNode,
Expand All @@ -14,6 +14,7 @@ import type {
// Config:
GraphQLEnumValueConfigMap,
GraphQLFieldConfigMap,
GraphQLObjectTypeExtensions,
// Resolvers:
GraphQLFieldResolver,
GraphQLInputFieldConfigMap,
Expand Down Expand Up @@ -69,11 +70,15 @@ export interface ObjectResolver<TSource = any, TContext = any> {
| ObjectFieldConfig<TSource, TContext>;
}

export interface ObjectPlan<TSource = any, TContext = any> {
export type ObjectPlan<TSource = any, TContext = any> = {
__assertStep?:
| ((step: ExecutableStep) => asserts step is ExecutableStep)
| { new (...args: any[]): ExecutableStep };
} & {
[key: string]:
| FieldPlanResolver<any, any, any>
| ObjectFieldConfig<TSource, TContext>;
}
};

export interface EnumResolver {
[key: string]: string | number | Array<any> | Record<string, any> | symbol;
Expand Down Expand Up @@ -412,6 +417,15 @@ export function makeExtendSchemaPlugin(
description,
}
: null),
...(plans?.[name]?.__assertStep
? {
extensions: {
grafast: {
assertStep: plans[name].__assertStep as any,
},
} as GraphQLObjectTypeExtensions<any, any>,
}
: null),
}),
uniquePluginName,
);
Expand Down

0 comments on commit 1dd9c30

Please sign in to comment.