Skip to content

Allow for complete messages to be captured in a subscription #9898

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions packages/pubsub/__tests__/AWSAppSyncRealTimeProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,60 @@ describe('AWSAppSyncRealTimeProvider', () => {
);
});

test('subscription is complete when a connection is formed and a complete message is received', async () => {
expect.assertions(1);

const mockComplete = jest.fn();
const observer = provider.subscribe('test', {
appSyncGraphqlEndpoint: 'ws://localhost:8080',
});

const subscription = observer.subscribe({
// Succeed only when the complete message comes through
complete: mockComplete,
// Closing a hot connection (for cleanup) makes it blow up the test stack
error: () => {},
});
await fakeWebSocketInterface?.standardConnectionHandshake();
await fakeWebSocketInterface?.sendDataMessage({
type: MESSAGE_TYPES.GQL_COMPLETE,
payload: {},
});

expect(mockComplete).toBeCalled();
});

test('subscription is complete when a connection is formed and a complete message is received after connection ack', async () => {
expect.assertions(1);

const mockComplete = jest.fn();
const observer = provider.subscribe('test', {
appSyncGraphqlEndpoint: 'ws://localhost:8080',
});

const subscription = observer.subscribe({
// Succeed only when the complete message comes through
complete: mockComplete,
// Closing a hot connection (for cleanup) makes it blow up the test stack
error: () => {},
});
await fakeWebSocketInterface?.standardConnectionHandshake();
await fakeWebSocketInterface?.sendMessage(
new MessageEvent('start_ack', {
data: JSON.stringify({
type: MESSAGE_TYPES.GQL_START_ACK,
payload: { connectionTimeoutMs: 100 },
}),
})
);
await fakeWebSocketInterface?.sendDataMessage({
type: MESSAGE_TYPES.GQL_COMPLETE,
payload: {},
});

expect(mockComplete).toBeCalled();
});

test('socket is closed when subscription is closed', async () => {
expect.assertions(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
subscriptionFailedCallback,
} = this.subscriptionObserverMap.get(id) || {};

logger.debug({ id, observer, query, variables });
logger.debug({ id, observer, query, variables, type });

if (type === MESSAGE_TYPES.GQL_DATA && payload && payload.data) {
if (observer) {
Expand All @@ -407,6 +407,14 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
return;
}

if (type === MESSAGE_TYPES.GQL_COMPLETE) {
logger.debug(`connection completed for id: ${id}`);
if (observer) {
observer.complete();
}
return;
}

if (type === MESSAGE_TYPES.GQL_START_ACK) {
logger.debug(
`subscription ready for ${JSON.stringify({ query, variables })}`
Expand Down
2 changes: 1 addition & 1 deletion packages/pubsub/src/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class PubSubClass {
start: console.error,
next: value => observer.next({ provider, value }),
error: error => observer.error({ provider, error }),
// complete: observer.complete, // TODO: when all completed, complete the outer one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this comment

complete: observer.complete, // TODO: when all completed, complete the outer one
})
);

Expand Down