Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): subscription operations are distinct on the message ID #24

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ The client is now **ready** to request subscription operations.

Direction: **Client -> Server**

Requests a operation specified in the message `payload`. This message leverages the unique ID field to connect future server messages to the operation started by this message.
Requests a operation specified in the message `payload`. This message provides a unique ID field to connect future server messages to the operation started by this message.

If there is already an active subscriber for a `subscription` operation matching the provided ID, the server will close the socket immediately with the event `4409: Subscriber for <unique-operation-id> already exists`. Since `query` and `mutation` resolve to a single emitted value, their subscription does not require reservations for additional future events. Having this in mind, the server may not assert this rule for these operations.

```typescript
import { DocumentNode } from 'graphql';
Expand Down
7 changes: 7 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ export function createServer(
if (operationAST.operation === 'subscription') {
const subscriptionOrResult = await subscribe(execArgs);
if (isAsyncIterable(subscriptionOrResult)) {
// iterable subscriptions are distinct on ID
if (ctx.subscriptions[message.id]) {
return ctx.socket.close(
4409,
`Subscriber for ${message.id} already exists`,
);
}
ctx.subscriptions[message.id] = subscriptionOrResult;

try {
Expand Down
58 changes: 58 additions & 0 deletions src/tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,64 @@ describe('Subscribe', () => {

expect(onMessageFn).toBeCalledTimes(3); // ack, next, complete
});

it('should close the socket on duplicate `subscription` operation subscriptions request', async () => {
expect.assertions(3);

await makeServer();

const client = new WebSocket(url, GRAPHQL_TRANSPORT_WS_PROTOCOL);
client.onopen = () => {
client.send(
stringifyMessage<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);
};

client.onmessage = ({ data }) => {
const message = parseMessage(data);
if (message.type === MessageType.ConnectionAck) {
client.send(
stringifyMessage<MessageType.Subscribe>({
id: 'not-unique',
type: MessageType.Subscribe,
payload: {
query: `subscription {
boughtBananas {
name
}
}`,
},
}),
);

// try subscribing with a live subscription id
setTimeout(() => {
client.send(
stringifyMessage<MessageType.Subscribe>({
id: 'not-unique',
type: MessageType.Subscribe,
payload: {
query: `subscription {
greetings
}`,
},
}),
);
}, 10);
}
};

await new Promise((resolve) => {
client.onclose = (event) => {
expect(event.code).toBe(4409);
expect(event.reason).toBe('Subscriber for not-unique already exists');
expect(event.wasClean).toBeTruthy();
resolve(); // done
};
});
});
});

describe('Keep-Alive', () => {
Expand Down