zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 623 forks source link

Custom connectFn - WebSocketChannel Socket<List<int>> #1271

Open GoncaloPT opened 1 year ago

GoncaloPT commented 1 year ago

Description While using graphql, I have specific requirement which force me to use a custom connectFn, so I can provide a custom WebSocketChannel. By creating 2 stream controllers ( one for upstream and other for downstream) and pass the sink and stream to the websocket channel, I'm able to implement custom networking. The problem resides in the way graphql interacts with the sink. From what seen in graphql(this library) code:

socketChannel!.sink.add(
          json.encode(
            message,
            toEncodable: (dynamic m) => m.toJson(),
          ),
        );

a String is sent to the sink ( the result of json.encode is a string). This clashes with the WebSocketChannel constructor signature, which clearly mandates a List of int:

WebSocketChannel(StreamChannel<List<int>> channel,
      {String? protocol, Duration? pingInterval, bool serverSide = true})
      : _webSocket = WebSocketImpl.fromSocket(
            channel.stream, channel.sink, protocol, serverSide)
          ..pingInterval = pingInterval;

In the stream connected to the sink, i receive a list of ints that are not convertible to string; and honestly, i don't know what they are. ( Perhaps some toString of the object hash?! ). Example message ( this should be the first ack sent to the server ):

flutter: ======================================================
flutter: PRINTING POSSIBLE CONVERSIONS FROM UPSTREAM MESSAGE
flutter: originalEvent: [129, 154, 77, 223, 32, 209]
flutter: base64Event: FormatException: Invalid character (at character 1)
šMß Ñ
^
flutter: obj: [129, 154, 77, 223, 32, 209]
flutter: obj: šMß Ñ

Reroduce A runnable example (flutter app) can be found at this repo. Just follow the instructions on the readme, or just run the project and look into the console.

Expected behavior I would expect the data that comes from the sink would be easily convertable to string. I would expect to see a string with json inside.

device / execution context Are you on iOS, android, web, in a simulator, running from the terminal etc? This is especially important for localhost connection issues.

vincenzopalazzo commented 1 year ago

OK I just add some more content here, this is expected, but maybe would be good to have some helper function to help to encode and to decode the response.

This issue is lock by the web socket refactoring