Skip to content
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
12 changes: 12 additions & 0 deletions packages/codegen/__tests__/gql-codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ describe('GraphQL Codegen — All Languages', () => {
expect(client.content).not.toContain('func (c *Gql) Pets(');
});

it('retries subscriptions when the shared WebSocket closes during a write', async () => {
const files = await generateForLanguage(language);
const client = getFile(files, 'gql-client')!;
expect(client.content).toContain('wasCurrent := c.wsConn == conn');
expect(client.content).toContain(
'shouldReconnect := wasCurrent && c.reconnect && !c.disposed',
);
expect(client.content).toContain('subscribeErr = conn.WriteJSON(');
expect(client.content).toContain('if c.wsConn == conn {');
expect(client.content).toContain('c.wsConn = nil');
});

it('generates unified result types instead of per-operation types', async () => {
const files = await generateForLanguage(language);
const types = getFile(files, 'gql-types')!;
Expand Down
54 changes: 29 additions & 25 deletions packages/codegen/src/languages/go/templates/graphql/client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ func (c *<%= it.clientClass %>) readWsMessages(conn *websocket.Conn) {
_, message, err := conn.ReadMessage()
if err != nil {
c.mu.Lock()
if c.wsConn == conn { c.wsConn = nil }
shouldReconnect := c.reconnect && !c.disposed && len(c.subs) > 0
wasCurrent := c.wsConn == conn
if wasCurrent { c.wsConn = nil }
shouldReconnect := wasCurrent && c.reconnect && !c.disposed && len(c.subs) > 0
c.mu.Unlock()
if shouldReconnect { go c.reconnectSubscriptions() }
return
Expand Down Expand Up @@ -372,16 +373,6 @@ func (c *<%= it.clientClass %>) Subscribe(fn func(*SubscriptionBuilder) *Subscri
c.mu.Lock()
defer c.mu.Unlock()

var connectErr error
for attempt := 0; ; attempt++ {
connectErr = c.ensureWsConn()
if connectErr == nil { break }
if !c.reconnect || attempt >= c.maxReconnectAttempts {
return nil, connectErr
}
time.Sleep(c.reconnectInterval)
}

id := fmt.Sprintf("%d", c.nextSubID)
c.nextSubID++

Expand All @@ -395,19 +386,32 @@ func (c *<%= it.clientClass %>) Subscribe(fn func(*SubscriptionBuilder) *Subscri
handler(&envelope.Data)
}}

c.writeMu.Lock()
err := c.wsConn.WriteJSON(map[string]interface{}{
"type": "subscribe",
"id": id,
"payload": map[string]interface{}{
"query": query,
"variables": variables,
},
})
c.writeMu.Unlock()
if err != nil {
delete(c.subs, id)
return nil, fmt.Errorf("ws subscribe: %w", err)
var subscribeErr error
for attempt := 0; ; attempt++ {
subscribeErr = c.ensureWsConn()
if subscribeErr == nil {
conn := c.wsConn
c.writeMu.Lock()
subscribeErr = conn.WriteJSON(map[string]interface{}{
"type": "subscribe",
"id": id,
"payload": map[string]interface{}{
"query": query,
"variables": variables,
},
})
c.writeMu.Unlock()
if subscribeErr == nil { break }
if c.wsConn == conn {
_ = conn.Close()
c.wsConn = nil
}
}
if !c.reconnect || attempt >= c.maxReconnectAttempts {
delete(c.subs, id)
return nil, fmt.Errorf("ws subscribe: %w", subscribeErr)
}
time.Sleep(c.reconnectInterval)
}

return func() {
Expand Down