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.24k stars 613 forks source link

How to wait for connection acknowledgement and then start subscription. #329

Open anirudhsharma392 opened 5 years ago

anirudhsharma392 commented 5 years ago
Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: GraphQLProvider(
        client: client,
        child: Subscription(operationName, subscription, builder: ({
          bool loading,
          dynamic payload,
          dynamic error,
        }) {
          if (loading==true) {
        return LoadingAnimation();

          }

          print("Subscription payload:  $payload");

          return Query(
              options: QueryOptions(
                document: playDuel,
                variables: {},
                pollInterval: 10000,
              ),
              builder: (QueryResult result, {VoidCallback refetch}) {
                if (result.errors != null) {
                  print(result.errors);
                }

                if (result.loading) {
                  return LoadingAnimation();
                }

                print(result.data['playDuel']);

                return Container();
              });
        }),
      ),
    );
  }

this loading function do not gives me live update about the connection. it always returns true. So all i want is to get data printed by Subscription widget on console get stored in some variable.

baptisteArno commented 4 years ago

I have the same problem. No info on websocket connection state with this client :

final sessionToken = await storage.read(key: 'session_token');
    AuthLink _authLink = AuthLink(
      getToken: () async => 'Bearer $sessionToken',
    );
    HttpLink _httpLink = HttpLink(
      uri: '$backendUrl/graphql',
    );
    print('je suis appelé');
    final WebSocketLink websocketLink = WebSocketLink(
      url: 'ws://12da74dc.ngrok.io/graphql',
      config: SocketClientConfig(
        autoReconnect: true,
        inactivityTimeout: Duration(seconds: 30),
      ),
    );
    Link _link = _authLink.concat(_httpLink).concat(websocketLink);

    client = ValueNotifier(GraphQLClient(
      cache: InMemoryCache(),
      link: _link,
    ));

And if I put anything for the websocket url it won't print an error. It doesn't say anything.

It works great with Queries and Mutations though