removeSupporter method Null safety

Future<Patient?> removeSupporter(
  1. {required Supporter supporter}
)

Removes supporter from the list of supporters from the currently logged in patient. Returns a new Patient instance (and publishes it) if successful, otherwise returns null.

Implementation

Future<Patient?> removeSupporter({required final Supporter supporter}) async {
  final gqlQueryString = '''
      mutation RemoveSupporterFromPatient {
        removeSupporterFromPatient(
          supporter: ${supporter.id}
        ) {
          ${PatientGqlEntity.gqlQueryFragment}
        }
      }
    ''';

  try {
    final queryResult = await graphQLClient.mutate<void>(MutationOptions(
      document: gql(gqlQueryString),
      fetchPolicy: FetchPolicy.networkOnly,
    ));

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return null;
    }

    final data = queryResult.data!['removeSupporterFromPatient']
        as Map<String, dynamic>;

    final patientGqlFragment = PatientGqlEntity.fromJson(data);

    if (patientGqlFragment != null) {
      final updatedPatient = patientGqlFragment.toPatient();
      _sendPatientUpdate(patient: updatedPatient);
      return updatedPatient;
    }
  } catch (e, stackTrace) {
    logger?.e('Could not remove supporter from patient', e, stackTrace);
    return null;
  }

  return null;
}