gql-dart / gql

Libraries supporting GraphQL in Dart
MIT License
268 stars 121 forks source link

[gql_web_socket_link] Can not hot reload or restart when using WebSocketLink #392

Closed GGGuenni closed 1 year ago

GGGuenni commented 1 year ago

I'm running on flutter web and using RiverPod

The GraphQLClient
```dart final gqlWebsocketClientProvider = Provider( (ref) { final String token = ref.read(authTokenProvider); final Link httpLink = HttpLink( apiHttpUrl, defaultHeaders: { if (token.isNotEmpty) 'Authorization': 'Bearer $token', }, ); final wsLink = WebSocketLink( apiWsUrl, config: SocketClientConfig( autoReconnect: true, inactivityTimeout: const Duration(seconds: 30), initialPayload: { 'headers': {'Authorization': 'Bearer $token'}, }, ), ); var link = Link.split((request) => request.isSubscription, wsLink, httpLink); final policies = Policies( fetch: FetchPolicy.networkOnly, ); final wsClient = GraphQLClient( cache: GraphQLCache(), link: link, defaultPolicies: DefaultPolicies( watchQuery: policies, query: policies, mutate: policies, ), ); return wsClient; }, name: 'gql websocket Provider', ); ```
Usage
```dart class AssetsNotifier extends ChangeNotifier { final Ref ref; late final GraphQLClient _wsClient = ref.read(gqlWebsocketClientProvider); late final Stream?> _stream; late final StreamSubscription _subscription; OperationException? _exception; List? _assets; AssetsNotifier(this.ref) { final SubscriptionOptions options = SubscriptionOptions( document: gql(_assetsSubscription), ); _stream = _wsClient.subscribe(options).map( (event) { if (event.hasException) { _exception = event.exception; return null; } return (event.data)?.map((e) => Asset.fromJson(e)).toList(); }, ); _subscription = _stream.listen((event) { _assets = event; notifyListeners(); }); ref.onDispose(() { _subscription.cancel(); }); } } ```

This error is spammed a few times when hot restarting/reloading or refreshing the page

Initialising connection
Got object store box in database graphqlclientstore.
--- Hot Restart happens here ---
Initialising connection
[SocketClient] message stream encountered error: WebSocketChannelException: WebSocket connection failed.
stacktrace:

Disconnected from websocket.
Error: WebSocketChannelException: WebSocket connection failed.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 299:10  createErrorWithStack
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 355:28            _throw
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/core/errors.dart 120:5                                           throwWithStackTrace
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1385:11                                          callback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15           <fn>

Any idea what could be causing this?