executeList<T> method

Future<List<T>> executeList<T>({
  1. required GQLOperationType operationType,
  2. required String document,
  3. Map<String, dynamic>? variable,
  4. FetchPolicy? fetchPolicy,
  5. required T modelParser(
    1. dynamic json
    ),
})

Implementation

Future<List<T>> executeList<T>({
  required GQLOperationType operationType,
  required String document,
  Map<String, dynamic>? variable,
  FetchPolicy? fetchPolicy,
  required T Function(dynamic json) modelParser,
}) async {
  try {
    late QueryResult response;
    if (operationType == GQLOperationType.query) {
      response = await _client.query(
        QueryOptions(
          document: gql(document),
          variables: variable ?? {},
          fetchPolicy: fetchPolicy ?? _gqlConfig.queryPolicy,
        ),
      );
    } else {
      response = await _client.mutate(
        MutationOptions(
          document: gql(document),
          variables: variable ?? {},
          fetchPolicy: fetchPolicy ?? _gqlConfig.mutationPolicy,
        ),
      );
    }

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

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

    final resolvedData = _getResolvedData(document, 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,
    );
  }
}