diff --git a/CHANGELOG.md b/CHANGELOG.md index 29475a738..fb62279ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ### vNEXT +- Operation key is now `string` instead of `number` [PR #176](https://github.com/apollographql/subscriptions-transport-ws/pull/176) - ... ### 0.7.1 diff --git a/src/client.ts b/src/client.ts index 5a7f81674..697956621 100644 --- a/src/client.ts +++ b/src/client.ts @@ -228,7 +228,7 @@ export class SubscriptionClient { return this.on('reconnecting', callback, context); } - public unsubscribe(opId: number) { + public unsubscribe(opId: string) { if (this.operations[opId]) { delete this.operations[opId]; this.sendMessage(opId, MessageTypes.GQL_STOP, undefined); @@ -237,7 +237,7 @@ export class SubscriptionClient { public unsubscribeAll() { Object.keys(this.operations).forEach( subId => { - this.unsubscribe(parseInt(subId, 10)); + this.unsubscribe(subId); }); } @@ -304,7 +304,7 @@ export class SubscriptionClient { } } - private executeOperation(options: OperationOptions, handler: (error: Error[], result?: any) => void): number { + private executeOperation(options: OperationOptions, handler: (error: Error[], result?: any) => void): string { const opId = this.generateOperationId(); this.operations[opId] = { options: options, handler }; @@ -324,7 +324,7 @@ export class SubscriptionClient { return opId; } - private buildMessage(id: number, type: string, payload: any) { + private buildMessage(id: string, type: string, payload: any) { const payloadToReturn = payload && payload.query ? { ...payload, @@ -362,7 +362,7 @@ export class SubscriptionClient { }]; } - private sendMessage(id: number, type: string, payload: any) { + private sendMessage(id: string, type: string, payload: any) { this.sendMessageRaw(this.buildMessage(id, type, payload)); } @@ -392,8 +392,8 @@ export class SubscriptionClient { } } - private generateOperationId() { - return ++this.nextOperationId; + private generateOperationId(): string { + return String(++this.nextOperationId); } private tryReconnect() { @@ -404,7 +404,7 @@ export class SubscriptionClient { if (!this.reconnecting) { Object.keys(this.operations).forEach((key) => { this.unsentMessagesQueue.push( - this.buildMessage(parseInt(key, 10), MessageTypes.GQL_START, this.operations[key].options), + this.buildMessage(key, MessageTypes.GQL_START, this.operations[key].options), ); }); this.reconnecting = true; @@ -462,7 +462,7 @@ export class SubscriptionClient { private processReceivedData(receivedData: any) { let parsedMessage: any; - let opId: number; + let opId: string; try { parsedMessage = JSON.parse(receivedData); @@ -478,6 +478,7 @@ export class SubscriptionClient { ].indexOf(parsedMessage.type) !== -1 && !this.operations[opId] ) { this.unsubscribe(opId); + return; } diff --git a/src/helpers.ts b/src/helpers.ts index 5b492ffdc..993aa4580 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -15,10 +15,10 @@ export function addGraphQLSubscriptions(networkInterface: any, wsClient: Subscri } return assign(networkInterface, { - subscribe(request: any, handler: any): number { + subscribe(request: any, handler: any): string { return wsClient.subscribe(request, handler); }, - unsubscribe(id: number): void { + unsubscribe(id: string): void { wsClient.unsubscribe(id); }, }); diff --git a/src/test/tests.ts b/src/test/tests.ts index 3cafe6b22..4f273e864 100644 --- a/src/test/tests.ts +++ b/src/test/tests.ts @@ -1727,7 +1727,7 @@ describe('Server', function () { it('does not send more subscription data after client unsubscribes', function (done) { const client4 = new SubscriptionClient(`ws://localhost:${TEST_PORT}/`); - let subId: number; + let subId: string; setTimeout(() => { client4.unsubscribe(subId); }, 50);