Skip to content

Commit

Permalink
feat(api-graphql): return entire payload in callback (#13980)
Browse files Browse the repository at this point in the history
feat(api-graphql): return entire payload in callback
  • Loading branch information
iartemiev authored Nov 4, 2024
1 parent c3c7dc5 commit d6eede3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/api-graphql/__tests__/AWSAppSyncEventProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,40 @@ describe('AppSyncEventProvider', () => {
'Connection failed: Retriable Test',
);
});

test('subscription observer is triggered when a connection is formed and a data message is received after connection ack', async () => {
expect.assertions(1);
const mockNext = jest.fn();

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

const event = JSON.stringify({ some: 'data' });

observer.subscribe({
// Succeed only when the first message comes through
next: mockNext,
// Closing a hot connection (for cleanup) makes it blow up the test stack
error: () => {},
});
await fakeWebSocketInterface?.standardConnectionHandshake();
await fakeWebSocketInterface?.startAckMessage({
connectionTimeoutMs: 100,
});
await fakeWebSocketInterface?.sendDataMessage({
id: fakeWebSocketInterface?.webSocket.subscriptionId,
type: MESSAGE_TYPES.DATA,
event,
});

// events callback returns entire message contents
expect(mockNext).toHaveBeenCalledWith({
id: fakeWebSocketInterface?.webSocket.subscriptionId,
type: MESSAGE_TYPES.DATA,
event: JSON.parse(event),
});
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class AWSAppSyncEventProvider extends AWSWebSocketProvider {
if (type === MESSAGE_TYPES.DATA && payload) {
const deserializedEvent = JSON.parse(payload);
if (observer) {
observer.next(deserializedEvent);
observer.next({ id, type, event: deserializedEvent });
} else {
this.logger.debug(`observer not found for id: ${id}`);
}
Expand Down

0 comments on commit d6eede3

Please sign in to comment.