Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

fix: do not send GQL_STOP after GQL_COMPLETED is received #775

Merged
merged 5 commits into from
Aug 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### vNEXT
- Do not send GQL_STOP when unsubscribing after GQL_COMPLETE is received [Issue #765](https://github.com/apollographql/subscriptions-transport-ws/issues/711)

### v0.9.17

- Bump `graphql` peer/dev deps. <br/>
Expand Down
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,9 @@ export class SubscriptionClient {
break;

case MessageTypes.GQL_COMPLETE:
this.operations[opId].handler(null, null);
const handler = this.operations[opId].handler;
delete this.operations[opId];
handler.call(this, null, null);
break;

case MessageTypes.GQL_ERROR:
Expand Down
27 changes: 27 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,33 @@ describe('Client', function () {
done();
});

it('should not send GQL_STOP on unsubscribe after operation received a GQL_COMPLETE', (done) => {
const client = new SubscriptionClient(`ws://localhost:${TEST_PORT}/`);
const sendMessageSpy = sinon.spy(client as any, 'sendMessage');

client.onConnected(() => {
const subscription = client.request({
query: `subscription { somethingChanged }`,
variables: {},
}).subscribe({
next: () => {
client.client.onmessage({
data: JSON.stringify({id: 1, type: MessageTypes.GQL_COMPLETE}),
});
},
complete: () => {
subscription.unsubscribe();
},
});
setTimeout(() => {
expect(sendMessageSpy.calledWith(undefined, 'connection_init', sinon.match.any)).to.be.true;
expect(sendMessageSpy.calledWith('1', 'start', sinon.match.any)).to.be.true;
expect(sendMessageSpy.calledWith('1', 'stop', sinon.match.any)).to.be.false;
done();
}, 1000);
});
});

it('should delete operation when receive a GQL_COMPLETE', (done) => {
const subscriptionsClient = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`);
subscriptionsClient.operations['1'] = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/empty-iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export const createEmptyIterable = (): EmptyIterable => {
[$$asyncIterator]() {
return this;
},
};
} as any;
};