Skip to content

Commit

Permalink
fix(server): don’t send an error if the client cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
divanise committed Feb 20, 2024
1 parent 1eed42a commit 369c486
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,10 @@ export function makeServer<
stringifyMessage<MessageType.Next>(nextMessage, replacer),
);
},
error: async (errors: readonly GraphQLError[]) => {
error: async (
errors: readonly GraphQLError[],
notifyClient: boolean,
) => {
let errorMessage: ErrorMessage = {
id,
type: MessageType.Error,
Expand All @@ -705,9 +708,10 @@ export function makeServer<
...errorMessage,
payload: maybeErrors,
};
await socket.send(
stringifyMessage<MessageType.Error>(errorMessage, replacer),
);
if (notifyClient)
await socket.send(
stringifyMessage<MessageType.Error>(errorMessage, replacer),
);
},
complete: async (notifyClient: boolean) => {
const completeMessage: CompleteMessage = {
Expand All @@ -730,7 +734,10 @@ export function makeServer<
const maybeExecArgsOrErrors = await onSubscribe?.(ctx, message);
if (maybeExecArgsOrErrors) {
if (areGraphQLErrors(maybeExecArgsOrErrors))
return await emit.error(maybeExecArgsOrErrors);
return await emit.error(
maybeExecArgsOrErrors,
id in ctx.subscriptions,
);
else if (Array.isArray(maybeExecArgsOrErrors))
throw new Error(
'Invalid return value from onSubscribe hook, expected an array of GraphQLError objects',
Expand Down Expand Up @@ -760,17 +767,21 @@ export function makeServer<
execArgs.document,
);
if (validationErrors.length > 0)
return await emit.error(validationErrors);
return await emit.error(
validationErrors,
id in ctx.subscriptions,
);
}

const operationAST = getOperationAST(
execArgs.document,
execArgs.operationName,
);
if (!operationAST)
return await emit.error([
new GraphQLError('Unable to identify operation'),
]);
return await emit.error(
[new GraphQLError('Unable to identify operation')],
id in ctx.subscriptions,
);

// if `onSubscribe` didn't specify a rootValue, inject one
if (!('rootValue' in execArgs))
Expand Down

0 comments on commit 369c486

Please sign in to comment.