mutate<T> method

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

Implementation

Future<T> mutate<T>({
  required String mutation,
  Map<String, dynamic>? variable,
  FetchPolicy? fetchPolicy,
  required T Function(dynamic json) modelParser,
}) async {
  try {
    final response = await _client.mutate(
      MutationOptions(
        document: gql(mutation),
        variables: variable ?? {},
        fetchPolicy: fetchPolicy ?? _gqlConfig.mutationPolicy,
      ),
    );

    // Check if there are GraphQL errors first
    if (response.hasException) {
      throw response.exception!;
    }

    // Check if data is null
    if (response.data == null) {
      throw Exception('GraphQL response data is null');
    }

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