queryCancellable<TParsed> method

CancellableOperation<QueryResult<TParsed>> queryCancellable<TParsed>(
  1. QueryOptions<TParsed> options
)

Executes a query that can be cancelled via the returned CancellableOperation.

This is a convenience method that creates a CancellationToken for you and returns both the result future and the token wrapped in a CancellableOperation.

{@tool snippet} Basic usage

final operation = client.queryCancellable(
  QueryOptions(
    document: gql('query { ... }'),
  ),
);

// Later, if you need to cancel:
operation.cancel();

// The result future completes with a QueryResult whose exception is set
final result = await operation.result;
if (result.exception?.linkException is CancelledException) {
  // Handle cancellation
}

{@end-tool}

Implementation

CancellableOperation<QueryResult<TParsed>> queryCancellable<TParsed>(
  QueryOptions<TParsed> options,
) {
  final cancellationToken = CancellationToken();
  final modifiedOptions = options.copyWithOptions(
    cancellationToken: cancellationToken,
  );
  return CancellableOperation(
    result: query(modifiedOptions),
    cancellationToken: cancellationToken,
  );
}