Skip to content

Commit

Permalink
allow customization of execution for subscription events and export t…
Browse files Browse the repository at this point in the history
…he default
  • Loading branch information
yaacovCR committed Jun 28, 2022
1 parent 2bbae47 commit 3f69fb3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export interface ExecutionInfo extends DocumentInfo {
fieldResolver: GraphQLFieldResolver<any, any>;
typeResolver: GraphQLTypeResolver<any, any>;
subscribeFieldResolver: GraphQLFieldResolver<any, any>;
subscriptionEventExecutor: (
exeInfo: ExecutionInfo,
payload: unknown,
) => PromiseOrValue<ExecutionResult>;
}

/**
Expand Down Expand Up @@ -169,6 +173,12 @@ export interface ExecutionArgs {
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
subscriptionEventExecutor?: Maybe<
(
exeInfo: ExecutionInfo,
payload: unknown,
) => PromiseOrValue<ExecutionResult>
>;
}

type FieldsExecutor = (
Expand Down Expand Up @@ -329,6 +339,7 @@ export function buildExecutionInfo(
fieldResolver,
typeResolver,
subscribeFieldResolver,
subscriptionEventExecutor,
} = args;

// If the schema used for execution is invalid, throw an error.
Expand Down Expand Up @@ -362,6 +373,8 @@ export function buildExecutionInfo(
fieldResolver: fieldResolver ?? defaultFieldResolver,
typeResolver: typeResolver ?? defaultTypeResolver,
subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver,
subscriptionEventExecutor:
subscriptionEventExecutor ?? defaultSubscriptionEventExecutor,
};
}

Expand Down Expand Up @@ -1244,13 +1257,19 @@ function mapSourceToResponse(
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
// "ExecuteQuery" algorithm, for which `execute` is also used.
return mapAsyncIterator(resultOrStream, (payload: unknown) =>
executeQuery({
...exeInfo,
rootValue: payload,
}),
exeInfo.subscriptionEventExecutor(exeInfo, payload),
);
}

export const defaultSubscriptionEventExecutor = (
exeInfo: ExecutionInfo,
payload: unknown,
): PromiseOrValue<ExecutionResult> =>
executeQuery({
...exeInfo,
rootValue: payload,
});

/**
* Implements the "CreateSourceEventStream" algorithm described in the
* GraphQL specification, resolving the subscription source event stream.
Expand Down
1 change: 1 addition & 0 deletions src/execution/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
executeSync,
defaultFieldResolver,
defaultTypeResolver,
defaultSubscriptionEventExecutor,
} from './execute';

export type {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ export {
executeSync,
defaultFieldResolver,
defaultTypeResolver,
defaultSubscriptionEventExecutor,
responsePathAsArray,
getArgumentValues,
getVariableValues,
Expand Down

0 comments on commit 3f69fb3

Please sign in to comment.