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 622 forks source link

Log polling WatchQuery request/response individual duration #1356

Open robertoestivill opened 1 year ago

robertoestivill commented 1 year ago

I'm creating a polling process using a WatchQuery in the following way

final options = WatchQueryOptions(
  document: gql(MyQuery.graphql),
  fetchPolicy: FetchPolicy.networkOnly,
  pollInterval: Duration(seconds: pollInterval),
  eagerlyFetchResults: true,
);

final pollingQuery = client.watchQuery(options);

_subscription = _pollingQuery.stream.listen(
  handleResult,
  onError: (Object? error) {
    logException(error);
  },
);

void handleResult(QueryResult<Object?> result) {
  ...    
}

I'm trying to understand how long does each of the individual queries total duration is in terms of roundtrip to the server , but I don't find any way to "hook" or "get a callback" for when this events happen.

Is it safe to assume that when result.isLoading == true the polling request has just kicked off?

Maybe something like this ?

void handleResult(QueryResult<Object?> result) {
  if (result.isLoading) {
    // start tracking here?
    // this could come from a cache right?
    ... 
    return;
  }

  // stop tracking here?

  // handle exception
  // handle data
}

Any tips or ideas are appreciated Thank you✌️

vincenzopalazzo commented 1 year ago

It would be good if you could run some experimentation with your use case and contribute back! It is time-consuming to setup an environment for me to produce your use case and implement a solution.

Your idea looks good to me btw

robertoestivill commented 1 year ago

I was just wondering if anyone could think of an API to use, rather than people trying to replicate my use case.

Yeah, I can post my results here, but sadly they are not looking very promising.

It seems like QueryResultSource.loading is only emitted the very first time handleResult is called, very likely when there's no results yet.

From that point on, handleResult is being called ONLY with QueryResultSource.network every time there's a polling request.

This means that QueryResult.isLoading is always false during polling and there's no event I can use to trigger the start of the measuring.

Will keep digging.

vincenzopalazzo commented 1 year ago

I was just wondering if anyone could think of an API to use, rather than people trying to replicate my use case.

Without know and read what is the use case it is very difficult to design an API, for this reason exist the PoC