# GraphQL 订阅错误：连接初始化超时。未收到 GQL_CONNECTION_INIT 消息。

- **ID:** `api/graphql-subscription-connection-init-timeout`
- **领域:** api
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 87%

## 根因

GraphQL 订阅的 WebSocket 连接已建立，但客户端未在服务器的超时窗口内（通常为 10-30 秒）发送必需的 GQL_CONNECTION_INIT 消息。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| graphql-ws 5.14 | active | — | — |
| subscriptions-transport-ws 0.11 | active | — | — |
| Apollo Server 4.10 | active | — | — |
| HotChocolate 13.0 | active | — | — |

## 解决方案

1. ```
   确保 GraphQL 客户端在 WebSocket 连接打开后立即发送 GQL_CONNECTION_INIT 消息。在 Apollo Client 中这是自动的，但如果使用自定义 WebSocket，请在 'open' 事件处理程序中发送。
   ```
2. ```
   使用自动处理握手的库，例如 graphql-ws 客户端或 Apollo Client 的订阅支持，而不是原始 WebSocket。
   ```

## 无效尝试

- **** — A longer timeout masks the real issue (missing init message) and may cause resource leaks from stale connections; the client should send init promptly. (80% 失败率)
- **** — Reconnection does not fix the missing init message; the same error will recur unless the client code is fixed to send GQL_CONNECTION_INIT. (90% 失败率)
