aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 241 forks source link

Flutter: GraphQL update query not working #2026

Closed anaya12345 closed 1 year ago

anaya12345 commented 2 years ago

Description

In flutte application, when I call Amplify.API.mutate for ModelMutations.update(), the query does not return an error however it is not updating the item. The result of the query is not an error but if you look at the values meant to have been updated. Here is my code:

DateTime curDateTime = DateTime.now();

  TemporalDate? todaysDate;
  // TemporalTime? currentTime;

  try {
    todaysDate = TemporalDate.fromString(
        DateFormat("yyyy-MM-dd").format(curDateTime));

    // currentTime =
    //     TemporalTime.fromString(DateFormat("HH:0").format(curDateTime));
  } catch (e) {
    safePrint("Error in parse date and time. $e");
  }
  safePrint("todaysDate = $todaysDate");

  QueryPredicateGroup? wherePredicate =
      QueryPredicateGroup(QueryPredicateGroupType.and, [
    DaywiseHealthData.PDATE.eq(todaysDate),
    // HourlyHealthData.PTIME.eq(currentTime),
    DaywiseHealthData.USERID.eq(currentUserId)
  ]);

  final requestGet = ModelQueries.list(DaywiseHealthData.classType,
      // limit: 1,
      where: wherePredicate);
  final responseGet = await Amplify.API.query(request: requestGet).response;
  List<DaywiseHealthData?>? dailyHealthDataExist = responseGet.data?.items;

  safePrint("dailyHealthDataExist = $dailyHealthDataExist");
  GraphQLRequest request;

  if (dailyHealthDataExist!.isNotEmpty) {
    DaywiseHealthData existRecord = dailyHealthDataExist.first!;

    request = ModelMutations.update(
        existRecord.copyWith(
            pdate: todaysDate,
            restingHeartRate: restingHeartRate,
            totalStepCount: stepCount,
            totalActiveMinutes: activeMinutes,
            totalCaloriesBurnt: caloriesBurnt,
            peakHeartRate: peakHeartRate,
            deviceInfo: await getDeviceInfo()),
        where: wherePredicate);
  } else {
    request = ModelMutations.create(DaywiseHealthData(
        // id: DateTime.now().microsecondsSinceEpoch.toString(),
        restingHeartRate: restingHeartRate,
        totalStepCount: stepCount,
        totalActiveMinutes: activeMinutes,
        totalCaloriesBurnt: caloriesBurnt,
        peakHeartRate: peakHeartRate,
        isSleepDataSaved: false,
        pdate: todaysDate,
        userid: currentUserId,
        deviceInfo: await getDeviceInfo()));
  }

  final response = await Amplify.API.mutate(request: request).response;
  final saveRes = response.data;
  if (saveRes == null) {
    safePrint('errors: ${response.errors}');
    return null;
  }
  safePrint("Daily health data record saved: ${response.data}");

Here create is working file, but for update, it returning the existing object values, updated values I am not getting in response.

Please guide asap, I am struggling for this from 2 days. I am waiting for some reply.

Categories

Steps to Reproduce

No response

Screenshots

No response

Platforms

Android Device/Emulator API Level

No response

Environment

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.0.1, on Microsoft Windows [Version 10.0.19044.1706], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.11.16)
[√] Android Studio (version 2021.2)
[√] Connected device (4 available)
[√] HTTP Host Availability

! Doctor found issues in 1 category.

Dependencies

Dart SDK 2.17.1
Flutter SDK 3.0.1
karecirc 1.0.0+1

dependencies:
- amplify_api 0.5.1 [amplify_api_plugin_interface amplify_core amplify_api_android amplify_api_ios collection flutter meta plugin_platform_interface]
- amplify_auth_cognito 0.5.1 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_auth_plugin_interface amplify_core collection flutter plugin
_platform_interface]
- amplify_authenticator 0.1.2 [amplify_auth_cognito amplify_auth_plugin_interface amplify_core amplify_flutter collection flutter flutter_localizations
intl]
- amplify_core 0.5.1 [collection date_time_format flutter meta plugin_platform_interface uuid]
- amplify_datastore 0.5.1 [flutter amplify_datastore_plugin_interface amplify_core plugin_platform_interface meta collection async]
- amplify_flutter 0.5.1 [amplify_analytics_plugin_interface amplify_api_plugin_interface amplify_auth_plugin_interface amplify_core amplify_datastore_pl
ugin_interface amplify_flutter_android amplify_flutter_ios amplify_storage_plugin_interface collection flutter json_annotation meta plugin_platform_inte
rface]
- badges 2.0.3 [flutter]
- charts_flutter 0.12.0 [charts_common collection flutter intl logging meta]
- cupertino_icons 1.0.5
- device_info_plus 4.0.1 [flutter device_info_plus_platform_interface device_info_plus_macos device_info_plus_linux device_info_plus_web device_info_plu
s_windows]
- fitbitter 1.1.1 [flutter flutter_web_auth dio logger intl flutter_secure_storage]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_background_service 2.1.3 [flutter flutter_background_service_platform_interface flutter_background_service_android flutter_background_service_
ios]
- flutter_switch 0.3.2 [flutter]
- http 0.13.4 [async http_parser meta path]
- intl 0.17.0 [clock path]
- oauth2_client 2.4.0 [flutter crypto flutter_secure_storage flutter_web_auth http meta random_string]
- pin_code_fields 7.4.0 [flutter]
- provider 6.0.3 [collection flutter nested]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferen
ces_platform_interface shared_preferences_web shared_preferences_windows]
- uni_links 0.5.1 [flutter uni_links_platform_interface uni_links_web]
- xml2json 5.3.3 [xml]

transitive dependencies:
- amplify_analytics_plugin_interface 0.5.1 [amplify_core flutter meta]
- amplify_api_android 0.5.1 [flutter]
- amplify_api_ios 0.5.1 [amplify_core flutter]
- amplify_api_plugin_interface 0.5.1 [amplify_core collection flutter json_annotation meta]
- amplify_auth_cognito_android 0.5.1 [flutter]
- amplify_auth_cognito_ios 0.5.1 [amplify_core flutter]
- amplify_auth_plugin_interface 0.5.1 [amplify_core flutter meta]
- amplify_datastore_plugin_interface 0.5.1 [flutter meta collection amplify_core]
- amplify_flutter_android 0.5.1 [flutter]
- amplify_flutter_ios 0.5.1 [amplify_core flutter]
- amplify_storage_plugin_interface 0.5.1 [flutter meta amplify_core]
- async 2.8.2 [collection meta]
- characters 1.2.0
- charcode 1.3.1
- charts_common 0.12.0 [collection intl logging meta vector_math]
- clock 1.1.0
- collection 1.16.0
- crypto 3.0.2 [typed_data]
- date_time_format 2.0.1
- device_info_plus_linux 2.1.1 [device_info_plus_platform_interface file flutter meta]
- device_info_plus_macos 2.2.3 [device_info_plus_platform_interface flutter]
- device_info_plus_platform_interface 2.3.0+1 [flutter meta plugin_platform_interface]
- device_info_plus_web 2.1.0 [device_info_plus_platform_interface flutter_web_plugins flutter]
- device_info_plus_windows 3.0.1 [device_info_plus_platform_interface ffi flutter win32]
- dio 4.0.6 [http_parser path]
- ffi 2.0.1
- file 6.1.2 [meta path]
- flutter_background_service_android 2.0.3 [flutter flutter_background_service_platform_interface]
- flutter_background_service_ios 2.1.0 [flutter flutter_background_service_platform_interface]
- flutter_background_service_platform_interface 2.0.0 [flutter platform plugin_platform_interface]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- flutter_secure_storage 5.0.2 [meta flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter
_secure_storage_web flutter_secure_storage_windows]
- flutter_secure_storage_linux 1.1.0 [flutter flutter_secure_storage_platform_interface]
- flutter_secure_storage_macos 1.1.0 [flutter flutter_secure_storage_platform_interface]
- flutter_secure_storage_platform_interface 1.0.0 [flutter plugin_platform_interface]
- flutter_secure_storage_web 1.0.2 [flutter flutter_web_plugins flutter_secure_storage_platform_interface js]
- flutter_secure_storage_windows 1.1.2 [flutter flutter_secure_storage_platform_interface]
- flutter_web_auth 0.4.1 [flutter flutter_web_plugins]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- js 0.6.4
- json_annotation 4.5.0 [meta]
- logger 1.1.0
- logging 1.0.2
- material_color_utilities 0.1.4
- meta 1.7.0
- nested 1.0.0 [flutter]
- path 1.8.1
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.0.4 [flutter platform plugin_platform_interface]
- path_provider_windows 2.1.0 [ffi flutter path path_provider_platform_interface win32]
- petitparser 5.0.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- random_string 2.3.1
- shared_preferences_android 2.0.12 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.0.0 [flutter]
- shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_span 1.8.2 [collection path term_glyph]
- string_scanner 1.1.0 [charcode source_span]
- term_glyph 1.2.0
- typed_data 1.3.1 [collection]
- uni_links_platform_interface 1.0.0 [flutter plugin_platform_interface]
- uni_links_web 0.1.0 [flutter flutter_web_plugins uni_links_platform_interface]
- uuid 3.0.6 [crypto]
- vector_math 2.1.2
- win32 2.7.0 [ffi]
- xdg_directories 0.2.0+1 [meta path process]
- xml 6.1.0 [collection meta petitparser]

Device

Nokia 6.1plus

OS

Android 11

CLI Version

9.1.0

Additional Context

No response

anaya12345 commented 2 years ago

any updates?

cshfang commented 2 years ago

Hi @anaya12345. I created an app locally using your model schema but was unable to reproduce any errors locally. I was able to successfully mutate an existing record and log it.

I see that you are using a predicate to query for the existing record in the first place - a couple questions:

  1. Is the query returning a result successfully?
  2. Once you have a record, why is it necessary to use the predicate again when creating the update request?

As a sanity check, could you:

  1. Double check that the if clause is being triggered (probably, since the alternative is creation)
  2. Share with me what is being logged? Is it just Daily health data record saved:?
  3. Try this out on Android emulator? I am unable to test this on a physical Nokia device, but we can try to eliminate that as a root cause if you are still experiencing this on a non-Nokia emulator
anaya12345 commented 1 year ago

solved using Graph QL query instead of API.mutate

momotaro1105 commented 1 year ago

is there any more detail on this? Experiencing similar issues

dorontal commented 1 year ago

I am also experiencing the same issue. Why was this issue closed? The solution above that led to this issue's closing was not really a solution but a workaround. As it stands, it seems that API.mutate is unable to do the updates for us.

When I use API.mutate to update - as described in the documentation, e.g, via this type of code:

Future<void> updateTodo(Todo originalTodo) async {
  final todoWithNewName = originalTodo.copyWith(name: 'new name');

  final request = ModelMutations.update(todoWithNewName);
  final response = await Amplify.API.mutate(request: request).response;
  safePrint('Response: $response');
}

Then the update finishes without error, but what is returned in response is the non-updated original object from the DB, i.e, the update does not fail but the result is as if we did not do the update at all, it is not taken into account and processed in the DB, the DB does not change. Please revive this issue until API.mutate works or until the documentation tells us to do things differently. Thanks!

dorontal commented 1 year ago

solved using Graph QL query instead of API.mutate

@anaya12345 Do you mean you solved this by using API.query instead of API.mutate? is it possible to share how you did this via a GraphQL query? Or if anybody has any links to the proper way of implementing this workaround (syntax) I would very much appreciate it.

himanshugupta0007 commented 11 months ago

I am facing this issue. Please help me if this issue is fixed. API.mutate did not update the model though _version is updated at model butnot values.

Sushil787 commented 1 month ago

Mine worked after disabling conflict detection .

  1. amplify update api
  2. Disable Conflict detection . then push the change
  3. amplify push