From e1b7f821d4727f70e64dd334e45f6c65a063adfd Mon Sep 17 00:00:00 2001 From: Oskar Thorin Date: Sat, 21 May 2022 00:45:15 +0200 Subject: [PATCH] fix(graphql): send connection_init message during handshake The implementation of `graphql-transport-ws` fails on the first message as it's only sent if the connection controller is in the `connected` state. For this protocol, the state is instead set to `handshake` during the first `_write()`. https://github.com/zino-hofmann/graphql-flutter/blob/02be959597f0ffe0c21a227a2d1fdb02b3f56831/packages/graphql/lib/src/links/websocket_link/websocket_client.dart#L250-L256 Without this, the connection fails with the following log: ``` flutter: Initialising connection flutter: There was an error causing connection lost: Bad state: No element flutter: Disconnected from websocket. ``` --- .../websocket_link/websocket_client.dart | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/graphql/lib/src/links/websocket_link/websocket_client.dart b/packages/graphql/lib/src/links/websocket_link/websocket_client.dart index b1e2e1f67..de8ce0c81 100644 --- a/packages/graphql/lib/src/links/websocket_link/websocket_client.dart +++ b/packages/graphql/lib/src/links/websocket_link/websocket_client.dart @@ -370,13 +370,18 @@ class SocketClient { } void _write(final GraphQLSocketMessage message) { - if (_connectionStateController.value == SocketConnectionState.connected) { - socketChannel!.sink.add( - json.encode( - message, - toEncodable: (dynamic m) => m.toJson(), - ), - ); + switch (_connectionStateController.value) { + case SocketConnectionState.connected: + case SocketConnectionState.handshake: + socketChannel!.sink.add( + json.encode( + message, + toEncodable: (dynamic m) => m.toJson(), + ), + ); + break; + default: + break; } }