save method

Future<LocatorApiResponse?> save({
  1. required String apiToken,
  2. required Uri uri,
  3. void onResponse(
    1. String statusCode
    )?,
})

save saves the locator input to the server It returns a LocatorApiResponse with the saved locator or errors if any

Implementation

Future<LocatorApiResponse?> save({
  /// [apiToken] is the API token to use for authentication. You can get one using the `login` mutation
  /// on the GraphQL API.
  required String apiToken,

  /// [uri] is the GraphQL endpoint to use
  required Uri uri,

  /// [onResponse] is the callback to call when the response is received
  void Function(String statusCode)? onResponse,
}) async {
  final connector = LayrzConnector(uri: uri);
  try {
    final response = await connector.perform(
      query: id == null ? addGraphqlMutation : editGraphqlMutation,
      variables: {'apiToken': apiToken, 'data': toJson()},
    );

    final data = response.data;
    if (data == null) {
      onResponse?.call('INTERNAL_ERROR');
      Log.error("layrz_models/LocatorInput/save(): No response from server");
      return null;
    }

    final result = id == null ? data['data']['addLocator'] : data['data']['editLocator'];
    if (result == null) {
      onResponse?.call('INTERNAL_ERROR');
      Log.error("layrz_models/LocatorInput/save(): No result from server");
      return null;
    }

    if (result['status'] != 'OK') {
      onResponse?.call(result['status']);
      return LocatorApiResponse(errors: Map<String, dynamic>.from(result['errors'] ?? {}));
    }

    return LocatorApiResponse(locator: Locator.fromJson(result['result']));
  } catch (e, stack) {
    Log.critical("layrz_models/LocatorInput/save(): General exception => $e\n$stack");
    return null;
  }
}