getUsedApplicableInterestsForContentType method Null safety

Future<BuiltSet<LocalizedType<Interest>>> getUsedApplicableInterestsForContentType(
  1. {ContentElementType? type,
  2. required LocalizedType<Language>? language}
)

Retrieves the list of used interest entities. NOTE: When specifying no locale, the defaultValue of the returned LocalizedType will be the default locale. However, when specifying a locale, the defaultValue of the returned LocalizedType will match the requested locale. All localizations will still be available, but it is important to note that the defaultValue depends on the requested locale.

Implementation

Future<BuiltSet<LocalizedType<Interest>>>
    getUsedApplicableInterestsForContentType({
  final ContentElementType? type,
  required final LocalizedType<Language>? language,
}) async {
  final requestedLocale = language?.defaultValue.isoCode ?? 'en';
  final gqlQueryString = '''
    query FetchUsedInterests {
      usedInterests(
        ${type != null ? 'contentElementType:"${type.name}"' : ''}
        ${language != null ? 'contentElementLocale:"$requestedLocale"' : ''}
      ) {
        ${InterestGqlEntity.gqlQueryFragment}
      }
    }
  ''';

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

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return BuiltSet<LocalizedType<Interest>>();
    }

    final data = queryResult.data!['usedInterests'] as List<dynamic>;

    final l = data
        .map((dynamic e) =>
            InterestGqlEntity.fromJson(e as Map<String, dynamic>))
        .where((element) => element != null)
        .map((element) => element!.toLocalizedTypeInterest())
        .toBuiltSet();

    return l;
  } catch (e, stackTrace) {
    logger?.e(
        'Could not retrieve the applicable interests for a content type',
        e,
        stackTrace);
    return BuiltSet<LocalizedType<Interest>>();
  }
}