From 46ddc978798346af307cb1c4f53eae504c95b26a Mon Sep 17 00:00:00 2001 From: Abayomi-fx Date: Mon, 31 Aug 2026 11:08:48 +0100 Subject: [PATCH] fix: Bug: `GraphQLIndexer.subscribe` sends `subscribe` before `co (#513) --- src/indexer.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/indexer.ts b/src/indexer.ts index 8986853..bf87424 100644 --- a/src/indexer.ts +++ b/src/indexer.ts @@ -307,6 +307,7 @@ export class GraphQLIndexer { return; } ws = socket; + let queuedSubscribeMessage: string | null = null; socket.onopen = () => { if (unsubscribed || this.isDestroyed) { @@ -316,16 +317,14 @@ export class GraphQLIndexer { reconnectAttempts = 0; try { socket.send(JSON.stringify({ type: 'connection_init' })); - socket.send( - JSON.stringify({ - id: subId, - type: 'subscribe', - payload: { - query: options.query, - variables, - }, - }) - ); + queuedSubscribeMessage = JSON.stringify({ + id: subId, + type: 'subscribe', + payload: { + query: options.query, + variables, + }, + }); } catch (err) { this.handleError(options.onError, err); } @@ -341,6 +340,14 @@ export class GraphQLIndexer { if (!raw || typeof raw !== 'object') return; const data = raw as GraphQLServerMessage; + if (data.type === 'connection_ack') { + if (queuedSubscribeMessage !== null) { + socket.send(queuedSubscribeMessage); + queuedSubscribeMessage = null; + } + return; + } + if (data.type === 'next' || data.type === 'data') { const payload = data.payload ?? data.data; options.onData(payload);