execute<T> method

Future<T> execute<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<T> execute<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) {
      throw Exception('GraphQL response data is null');
    }

    final resolvedData = _getResolvedData(document, response.data!);
    try {
      return modelParser(resolvedData);
    } catch (e, s) {
      log("Model parsing error $e,\n$s");
      rethrow;
    }
  } catch (exception) {
    throw GQLException.fromException(
      exception,
      exceptionProviders: _gqlConfig.exceptionProviders,
    );
  }
}