Skip to content

Commit

Permalink
fix(server): scoped execution result formatter from onConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Aug 26, 2020
1 parent e81747b commit f91fadb
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/server.ts
Expand Up @@ -37,6 +37,11 @@ import {
} from './utils';
import { UUID } from './types';

export type ExecutionResultFormatter = (
ctx: Context,
result: ExecutionResult,
) => Promise<ExecutionResult> | ExecutionResult;

export interface ServerOptions {
/**
* The GraphQL schema on which the operations
Expand Down Expand Up @@ -105,23 +110,26 @@ export interface ServerOptions {
/**
* Format the operation execution results
* if the implementation requires an adjusted
* result.
* result. This formatter is run BEFORE the
* `onConnect` scoped formatter.
*/
formatExecutionResult?: (
ctx: Context,
result: ExecutionResult,
) => Promise<ExecutionResult> | ExecutionResult;
formatExecutionResult?: ExecutionResultFormatter;
/**
* The subscribe callback executed before
* the actual operation execution. Useful
* for manipulating the execution arguments
* before the doing the operation.
* before the doing the operation. As a second
* item in the array, you can pass in a scoped
* execution result formatter. This formatter
* is run AFTER the root `formatExecutionResult`.
*/
onSubscribe?: (
ctx: Context,
message: SubscribeMessage,
args: Optional<ExecutionArgs, 'schema'>,
) => Promise<ExecutionArgs> | ExecutionArgs;
) =>
| Promise<[ExecutionArgs, ExecutionResultFormatter?]>
| [ExecutionArgs, ExecutionResultFormatter?];
/**
* The complete callback is executed after the
* operation has completed or the subscription
Expand Down Expand Up @@ -330,12 +338,14 @@ export function createServer(
: operation.query,
variableValues: operation.variables,
};

let executionResultFormatter: ExecutionResultFormatter | undefined;

if (onSubscribe) {
execArgsMaybeSchema = await onSubscribe(
ctx,
message,
[
execArgsMaybeSchema,
);
executionResultFormatter,
] = await onSubscribe(ctx, message, execArgsMaybeSchema);
}
if (!execArgsMaybeSchema.schema) {
// not providing a schema is a fatal server error
Expand Down Expand Up @@ -377,9 +387,14 @@ export function createServer(

try {
for await (let result of subscriptionOrResult) {
// use the root formater first
if (formatExecutionResult) {
result = await formatExecutionResult(ctx, result);
}
// use the subscription specific formatter
if (executionResultFormatter) {
result = await executionResultFormatter(ctx, result);
}
await sendMessage<MessageType.Next>(ctx, {
id: message.id,
type: MessageType.Next,
Expand Down

0 comments on commit f91fadb

Please sign in to comment.