queryList<T> method

Future<List<T>> queryList<T>({
  1. required String query,
  2. Map<String, dynamic>? variable,
  3. FetchPolicy? fetchPolicy,
  4. required T modelParser(
    1. dynamic json
    ),
})

Implementation

Future<List<T>> queryList<T>({
  required String query,
  Map<String, dynamic>? variable,
  FetchPolicy? fetchPolicy,
  required T Function(dynamic json) modelParser,
}) async {
  try {
    final response = await _client.query(
      QueryOptions(
        document: gql(query),
        variables: variable ?? {},
        fetchPolicy: fetchPolicy ?? _gqlConfig.queryPolicy,
      ),
    );

    if (response.hasException) {
      throw response.exception!;
    }

    if (response.data == null) {
      return <T>[];
    }

    final resolvedData = _getResolvedData(query, response.data!);

    if (resolvedData is List) {
      try {
        return resolvedData
            .map((json) => modelParser(json))
            .toList()
            .cast<T>();
      } catch (e, s) {
        log("Model parsing error $e,\n$s");
        rethrow;
      }
    }
    return <T>[];
  } catch (exception) {
    throw GQLException.fromException(
      exception,
      exceptionProviders: _gqlConfig.exceptionProviders,
    );
  }
}