Skip to content

Commit

Permalink
feat(hooks): add postgraphile:liveSubscribe:executionResult hook (#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed May 26, 2021
1 parent 95a3451 commit 73fe801
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/postgraphile/http/liveSubscribe.ts
Expand Up @@ -32,10 +32,12 @@ type mixed = any;
*
* @internal
*/
export function makeLiveSubscribe(_params: {
export function makeLiveSubscribe(params: {
options: CreateRequestHandlerOptions;
pluginHook: PluginHookFn;
}): typeof subscribe {
const { pluginHook } = params;

return function liveSubscribe(
argsOrSchema: any | GraphQLSchema,
document?: DocumentNode,
Expand Down Expand Up @@ -106,7 +108,7 @@ export function makeLiveSubscribe(_params: {
* that it can clean up old listeners; we do this with the `finally` block.
*/
try {
return await execute(
const executionResult = execute(
schema,
document,
payload,
Expand All @@ -115,6 +117,23 @@ export function makeLiveSubscribe(_params: {
operationName,
fieldResolver,
);

const hookedExecutionResult = pluginHook(
'postgraphile:liveSubscribe:executionResult',
executionResult,
{
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
},
);

return await hookedExecutionResult;
} finally {
if (payload && typeof payload.release === 'function') {
payload.release();
Expand Down
27 changes: 27 additions & 0 deletions src/postgraphile/pluginHook.ts
Expand Up @@ -13,6 +13,8 @@ import { Extra as GraphQLWSContextExtra } from 'graphql-ws/lib/use/ws';
import { ExecutionParams } from 'subscriptions-transport-ws';
import { PostGraphileResponse } from './http/frameworks';

type PromiseOrValue<T> = T | Promise<T>;

// tslint:disable-next-line no-any
export type HookFn<TArg, TContext = any> = (arg: TArg, context: TContext) => TArg;
export type PluginHookFn = <TArgument, TContext = Record<string, any>>(
Expand Down Expand Up @@ -99,6 +101,31 @@ export interface PostGraphilePlugin {
}
>;

'postgraphile:liveSubscribe:executionResult'?: HookFn<
PromiseOrValue<
graphql.ExecutionResult<
{
[key: string]: any;
},
{
[key: string]: any;
}
>
>,
{
schema: graphql.GraphQLSchema;
document: graphql.DocumentNode;
rootValue: any;
contextValue: any;
variableValues: { [key: string]: any } | undefined;
operationName: string | undefined;
fieldResolver: graphql.GraphQLFieldResolver<any, any, { [argName: string]: any }> | undefined;
subscribeFieldResolver:
| graphql.GraphQLFieldResolver<any, any, { [argName: string]: any }>
| undefined;
}
>;

withPostGraphileContext?: HookFn<WithPostGraphileContextFn>;
}
type HookName = keyof PostGraphilePlugin;
Expand Down

0 comments on commit 73fe801

Please sign in to comment.