stream<T, U extends JsonSerializable> method

Stream<GraphQLResponse<T>> stream<T, U extends JsonSerializable>(
  1. GraphQLQuery<T, U> query, {
  2. Context context = const Context(),
})

Streams a GraphQLQuery, returning a typed response stream.

Useful for subscriptions or when using CachePolicy.cacheAndNetwork which yields cached data first, then network data.

Example:

client.stream(
  query,
  context: Context().withCachePolicy(CachePolicy.cacheAndNetwork),
).listen((response) {
  // First response: cached data (if available)
  // Second response: fresh network data
});

Implementation

Stream<GraphQLResponse<T>> stream<T, U extends JsonSerializable>(
  GraphQLQuery<T, U> query, {
  Context context = const Context(),
}) {
  final request = Request(
    operation: Operation(
      document: query.document,
      operationName: query.operationName,
    ),
    variables: query.getVariablesMap(),
    context: context,
  );

  return _link
      .request(request)
      .map(
        (response) => GraphQLResponse<T>(
          data: response.data == null
              ? null
              : query.parse(response.data ?? {}),
          errors: response.errors,
          context: response.context,
        ),
      );
}