Skip to content

Commit 3f69fb3

Browse files
committed
allow customization of execution for subscription events and export the default
1 parent 2bbae47 commit 3f69fb3

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/execution/execute.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export interface ExecutionInfo extends DocumentInfo {
125125
fieldResolver: GraphQLFieldResolver<any, any>;
126126
typeResolver: GraphQLTypeResolver<any, any>;
127127
subscribeFieldResolver: GraphQLFieldResolver<any, any>;
128+
subscriptionEventExecutor: (
129+
exeInfo: ExecutionInfo,
130+
payload: unknown,
131+
) => PromiseOrValue<ExecutionResult>;
128132
}
129133

130134
/**
@@ -169,6 +173,12 @@ export interface ExecutionArgs {
169173
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
170174
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
171175
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
176+
subscriptionEventExecutor?: Maybe<
177+
(
178+
exeInfo: ExecutionInfo,
179+
payload: unknown,
180+
) => PromiseOrValue<ExecutionResult>
181+
>;
172182
}
173183

174184
type FieldsExecutor = (
@@ -329,6 +339,7 @@ export function buildExecutionInfo(
329339
fieldResolver,
330340
typeResolver,
331341
subscribeFieldResolver,
342+
subscriptionEventExecutor,
332343
} = args;
333344

334345
// If the schema used for execution is invalid, throw an error.
@@ -362,6 +373,8 @@ export function buildExecutionInfo(
362373
fieldResolver: fieldResolver ?? defaultFieldResolver,
363374
typeResolver: typeResolver ?? defaultTypeResolver,
364375
subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver,
376+
subscriptionEventExecutor:
377+
subscriptionEventExecutor ?? defaultSubscriptionEventExecutor,
365378
};
366379
}
367380

@@ -1244,13 +1257,19 @@ function mapSourceToResponse(
12441257
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
12451258
// "ExecuteQuery" algorithm, for which `execute` is also used.
12461259
return mapAsyncIterator(resultOrStream, (payload: unknown) =>
1247-
executeQuery({
1248-
...exeInfo,
1249-
rootValue: payload,
1250-
}),
1260+
exeInfo.subscriptionEventExecutor(exeInfo, payload),
12511261
);
12521262
}
12531263

1264+
export const defaultSubscriptionEventExecutor = (
1265+
exeInfo: ExecutionInfo,
1266+
payload: unknown,
1267+
): PromiseOrValue<ExecutionResult> =>
1268+
executeQuery({
1269+
...exeInfo,
1270+
rootValue: payload,
1271+
});
1272+
12541273
/**
12551274
* Implements the "CreateSourceEventStream" algorithm described in the
12561275
* GraphQL specification, resolving the subscription source event stream.

src/execution/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
executeSync,
66
defaultFieldResolver,
77
defaultTypeResolver,
8+
defaultSubscriptionEventExecutor,
89
} from './execute';
910

1011
export type {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ export {
315315
executeSync,
316316
defaultFieldResolver,
317317
defaultTypeResolver,
318+
defaultSubscriptionEventExecutor,
318319
responsePathAsArray,
319320
getArgumentValues,
320321
getVariableValues,

0 commit comments

Comments
 (0)