fetchUserId method Null safety

  1. @protected
Future<String?> fetchUserId(
  1. String firebaseUid
)

Utility function that fetches a user's ID based on a given patient instance. This is required because Strapi does not support direct access to the underlying User instance by a where clause. This method can be removed as soon as Strapi exposes a custom resolver to store the unified data structure. Returns null if the user instance could not be found.

Implementation

@protected
Future<String?> fetchUserId(String firebaseUid) async {
  final gqlQueryString = '''
    query FetchUserId {
      users(
        where: {
          firebaseUid: "$firebaseUid"
        }
      ) {
        id
      }
    }
  ''';

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

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

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

    if (data.isNotEmpty) {
      return (data.first as Map<String, dynamic>)['id'] as String;
    }

    return null;
  } catch (e, stackTrace) {
    logger?.e('Could not fetch a user by its Firebase UID', e, stackTrace);
    return null;
  }
}