Skip to content

Commit

Permalink
fix(server): Close socket if onSubscribe returns invalid array (eni…
Browse files Browse the repository at this point in the history
…sdenjo#53)

* fix: throw useful error if onSubscribe returns invalid array

* refactor: misconfiguration is serious

* test: close socket

Co-authored-by: enisdenjo <badurinadenis@gmail.com>
  • Loading branch information
benjie and enisdenjo committed Nov 4, 2020
1 parent 70317b2 commit 0464a54
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/server.ts
Expand Up @@ -552,6 +552,10 @@ export function createServer(
if (maybeExecArgsOrErrors) {
if (areGraphQLErrors(maybeExecArgsOrErrors)) {
return await emit.error(maybeExecArgsOrErrors);
} else if (Array.isArray(maybeExecArgsOrErrors)) {
throw new Error(
'Invalid return value from onSubscribe hook, expected an array of GraphQLError objects',
);
}
// not errors, is exec args
execArgs = maybeExecArgsOrErrors;
Expand Down
38 changes: 38 additions & 0 deletions src/tests/server.ts
Expand Up @@ -941,6 +941,44 @@ describe('Subscribe', () => {
}, 30);
});

it('should close the socket on empty arrays returned from `onSubscribe`', async () => {
const { url } = await startTServer({
onSubscribe: () => {
return [];
},
});

const client = await createTClient(url);

client.ws.send(
stringifyMessage<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);

await client.waitForMessage(({ data }) => {
expect(parseMessage(data).type).toBe(MessageType.ConnectionAck);
});

client.ws.send(
stringifyMessage<MessageType.Subscribe>({
id: '1',
type: MessageType.Subscribe,
payload: {
query: 'subscription { ping }',
},
}),
);

await client.waitForClose((event) => {
expect(event.code).toBe(4400);
expect(event.reason).toBe(
'Invalid return value from onSubscribe hook, expected an array of GraphQLError objects',
);
expect(event.wasClean).toBeTruthy();
});
});

it('should use the execution result returned from `onNext`', async () => {
const { url } = await startTServer({
onNext: (_ctx, _message) => {
Expand Down

0 comments on commit 0464a54

Please sign in to comment.