watchQuery<TParsed> method Null safety

ObservableQuery<TParsed> watchQuery<TParsed>(
  1. WatchQueryOptions<TParsed> options
)

This registers a query in the QueryManager and returns an ObservableQuery based on the provided WatchQueryOptions.

{@tool snippet} Basic usage

final observableQuery = client.watchQuery(
  WatchQueryOptions(
    document: gql(
      r'''
      query HeroForEpisode($ep: Episode!) {
        hero(episode: $ep) {
          name
        }
      }
      ''',
    ),
    variables: {'ep': 'NEWHOPE'},
  ),
);

/// Listen to the stream of results. This will include:
/// * `options.optimisitcResult` if passed
/// * The result from the server (if `options.fetchPolicy` includes networking)
/// * rebroadcast results from edits to the cache
observableQuery.stream.listen((QueryResult result) {
  if (!result.isLoading && result.data != null) {
    if (result.hasException) {
      print(result.exception);
      return;
    }
    if (result.isLoading) {
      print('loading');
      return;
    }
    doSomethingWithMyQueryResult(myCustomParser(result.data));
  }
});
// ... cleanup:
observableQuery.close();

{@end-tool}

Implementation

ObservableQuery<TParsed> watchQuery<TParsed>(
    WatchQueryOptions<TParsed> options) {
  final policies = defaultPolicies.watchQuery.withOverrides(options.policies);
  return queryManager.watchQuery(options.copyWithPolicies(policies));
}