Skip to content

Commit

Permalink
fix(graphql): send connection_init message during handshake
Browse files Browse the repository at this point in the history
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.
```
  • Loading branch information
othorin authored May 20, 2022
1 parent 02be959 commit e1b7f82
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit e1b7f82

Please # to comment.