mutateMutation<DataType, ErrorType, VariablesType> method

Future<DataType?> mutateMutation<DataType, ErrorType, VariablesType>(
  1. String key,
  2. VariablesType variables, {
  3. MutationFn<DataType, VariablesType>? mutationFn,
  4. RetryConfig? retryConfig,
  5. List<String> refreshQueries = const [],
  6. List<String> refreshInfiniteQueries = const [],
})

Finds the Mutation with the given key and runs Mutation.mutate

It'll return the mutation result and will return null if fails

Optionally takes an mutationFn to override the existing mutationFn or create a completely new Mutation if doesn't exist Same situation for retryConfig

  • refreshQueries can be used to refresh queries after mutation
  • refreshInfiniteQueries can be used to refresh infinite queries after mutation

Implementation

Future<DataType?> mutateMutation<DataType, ErrorType, VariablesType>(
  String key,
  VariablesType variables, {
  MutationFn<DataType, VariablesType>? mutationFn,
  RetryConfig? retryConfig,
  List<String> refreshQueries = const [],
  List<String> refreshInfiniteQueries = const [],
}) async {
  try {
    DataType? result;
    final completer = Completer<DataType>();
    final mutation = getMutation<DataType, ErrorType, VariablesType>(
          key,
        ) ??
        (mutationFn != null
            ? createMutation<DataType, ErrorType, VariablesType>(
                key,
                mutationFn,
                retryConfig: retryConfig,
              )
            : null);

    final subscription = mutation?.dataStream.listen((data) {
      if (!completer.isCompleted) completer.complete(data);
    });

    final errorSubscription = mutation?.errorStream.listen((error) {
      if (!completer.isCompleted)
        completer.completeError(error != null ? error : "");
    });

    result = await mutation?.mutate(variables);
    result ??= await completer.future;

    errorSubscription?.cancel();
    subscription?.cancel();

    await this.refreshQueries(refreshQueries);
    await refreshInfiniteQueriesAllPages(refreshInfiniteQueries);
    return result;
  } catch (e) {
    return null;
  }
}