amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Amplify.API.query with QueryPredicate in where always returns empty when limit is provided

Open anaya12345 opened this issue 2 years ago • 6 comments

Description

I am trying to get list or single record by send QueryPredicate for where argument and limit as 1, but it always returns empty list, where as if single parameter provided then it returns data. But I want to get conditional data. Please guide to solve this issue asap.

Categories

  • [ ] Analytics
  • [ ] API (REST)
  • [X] API (GraphQL)
  • [ ] Auth
  • [ ] Authenticator
  • [ ] DataStore
  • [ ] Storage

Steps to Reproduce

No response

Screenshots

No response

Platforms

  • [ ] iOS
  • [X] Android
  • [ ] Web
  • [ ] macOS
  • [ ] Windows
  • [ ] Linux

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 avatar Aug 05 '22 09:08 anaya12345

Hello - can you provide a code sample for the query you are running?

Have you confirmed that if you remove the limit that data is returned?

Jordan-Nelson avatar Aug 05 '22 11:08 Jordan-Nelson

Yes, I have confirmed, below is the code:

QueryPredicateGroup? wherePredicate = QueryPredicateGroup(
    QueryPredicateGroupType.and,
    [DailyHealthData.PDATE.eq(todaysDate), DailyHealthData.USERID.eq(currentUserId)]);
final requestGet = ModelQueries.list(DailyHealthData.classType,
    // limit: limit,
    where: wherePredicate);
final responseGet = await Amplify.API.query(request: requestGet).response;
List<DailyHealthData?> dailyHealthDataList =
    (responseGet.data?.items)!.toList();

anaya12345 avatar Aug 05 '22 17:08 anaya12345

Also can we added sort option while getting list?

anaya12345 avatar Aug 05 '22 17:08 anaya12345

any updates?

anaya12345 avatar Aug 10 '22 12:08 anaya12345

@anaya12345 the response you are seeing is expected by AppSync’s design, however this isn’t intuitive so let me briefly explain.

The limit parameter is an upper limit. When AppSync is provided a query it will scan the table for items with this upper limit. It’s implemented this way for performance on large data sets.

If this is a common query that will be executed frequently we recommend setting up a Global Secondary Index (GSI). This will allow you to query your GSI index directly. Then in a single query you can get the full result you want and it will be more performant.

A more detailed write up on this behavior can be found within this issue: https://github.com/aws-amplify/amplify-js/issues/10085#issuecomment-1202622571

As for adding a sort option, this is also dependent on a GSI. We provide a guide on adding this feature here: https://docs.amplify.aws/guides/api-graphql/query-with-sorting/q/platform/flutter/

Equartey avatar Aug 10 '22 14:08 Equartey

oh, okk. I thought limit parameter is the number of records limit.

for sorting option, how can we do it in flutter?

anaya12345 avatar Aug 10 '22 18:08 anaya12345

To perform sorting in flutter you can take a look at the guide I linked above. The linked GH issue has more details on GSI. Let us know if you have any other questions.

Equartey avatar Mar 28 '23 16:03 Equartey

@anaya12345 - I am going to close this out since it looks like your question was answered and we have not heard back

Jordan-Nelson avatar Apr 03 '23 16:04 Jordan-Nelson