genericFetchFunction function

Future genericFetchFunction({
  1. required StreamController streamController,
  2. required BuildContext context,
  3. required String queryString,
  4. required Map<String, dynamic> variables,
  5. required String logTitle,
  6. String? logDescription,
})

Generic Fetch Function a generic fetch function for fetching all the problems, allergies medications, tests and diagnoses for the current patient in an episode

it takes in a String queryString, the Map of the query variables variables, the BuildContext context, and a stream controller streamController in which the data is added to

it then updates the stream controller with the returned data (if any) or an error if there was an error

Implementation

Future<dynamic> genericFetchFunction({
  required StreamController<dynamic> streamController,
  required BuildContext context,
  required String queryString,
  required Map<String, dynamic> variables,
  required String logTitle,
  String? logDescription,
}) async {
  streamController.add(<String, dynamic>{'loading': true});

  final IGraphQlClient kClient = AppWrapperBase.of(context)!.graphQLClient;

  /// fetch the data from the api
  final http.Response response = await kClient.query(
    queryString,
    variables,
  );

  final Map<String, dynamic> payLoad = kClient.toMap(response);
  final String? error = parseError(payLoad);

  if (error != null) {
    return streamController
        .addError(<String, dynamic>{'error': kClient.parseError(payLoad)});
  }

  return (payLoad['data'] != null)
      ? streamController.add(payLoad['data'])
      : streamController.add(null);
}