aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

DataStore.query should filter out deleted records #8759

Open elilambnz opened 3 years ago

elilambnz commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

DataStore

Amplify Categories

Not applicable

Environment information

``` # Put output below this line System: OS: macOS 11.5.1 CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz Memory: 3.17 GB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 12.18.2 - /usr/local/bin/node Yarn: 1.22.5 - ~/.yarn/bin/yarn npm: 7.18.1 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Browsers: Chrome: 92.0.4515.131 Firefox: 68.0.2 Firefox Developer Edition: 81.0 Safari: 14.1.2 npmPackages: @aws-amplify/ui-react: ^1.2.4 => 1.2.4 @capacitor/app: 1.0.2 => 1.0.2 @capacitor/cli: 3.0.2 => 3.0.2 @capacitor/core: 3.0.2 => 3.0.2 @capacitor/haptics: 1.0.2 => 1.0.2 @capacitor/keyboard: 1.0.2 => 1.0.2 @capacitor/status-bar: 1.0.2 => 1.0.2 @ionic/react: ^5.5.0 => 5.6.10 @ionic/react-router: ^5.5.0 => 5.6.10 @koale/useworker: ^4.0.2 => 4.0.2 @testing-library/jest-dom: ^5.11.9 => 5.14.1 @testing-library/react: ^11.2.5 => 11.2.7 @testing-library/user-event: ^12.6.3 => 12.8.3 @types/jest: ^26.0.20 => 26.0.23 @types/lodash: ^4.14.171 => 4.14.171 @types/node: ^12.19.15 => 12.20.15 @types/react: ^16.14.3 => 16.14.8 @types/react-dom: ^16.9.10 => 16.9.13 @types/react-router: ^5.1.11 => 5.1.15 @types/react-router-dom: ^5.1.7 => 5.1.7 @types/recoilize: ^0.8.0 => 0.8.0 @types/uuid: ^8.3.0 => 8.3.0 aws-amplify: ^4.1.2 => 4.1.2 ionicons: ^5.4.0 => 5.5.2 ionicons-loader: undefined () ionicons/components: undefined () ionicons/icons: 5.5.2 js-logger: ^1.6.1 => 1.6.1 moment: ^2.29.1 => 2.29.1 react: ^17.0.1 => 17.0.2 (17.0.1) react-dom: ^17.0.1 => 17.0.2 react-router: ^5.2.0 => 5.2.0 react-router-dom: ^5.2.0 => 5.2.0 react-scripts: 4.0.2 => 4.0.2 recoil: ^0.3.1 => 0.3.1 recoilize: ^2.0.1 => 2.0.1 typescript: ^4.1.3 => 4.3.4 uuid: ^8.3.2 => 8.3.2 (3.4.0, 3.3.2) web-vitals: ^0.2.4 => 0.2.4 workbox-background-sync: ^5.1.4 => 5.1.4 workbox-broadcast-update: ^5.1.4 => 5.1.4 workbox-cacheable-response: ^5.1.4 => 5.1.4 workbox-core: ^5.1.4 => 5.1.4 workbox-expiration: ^5.1.4 => 5.1.4 workbox-google-analytics: ^5.1.4 => 5.1.4 workbox-navigation-preload: ^5.1.4 => 5.1.4 workbox-precaching: ^5.1.4 => 5.1.4 workbox-range-requests: ^5.1.4 => 5.1.4 workbox-routing: ^5.1.4 => 5.1.4 workbox-strategies: ^5.1.4 => 5.1.4 workbox-streams: ^5.1.4 => 5.1.4 workerize: ^0.1.8 => 0.1.8 npmGlobalPackages: @aws-amplify/cli: 5.2.0 @ionic/cli: 6.16.3 eslint: 7.28.0 expo-cli: 4.7.1 grunt-cli: 1.3.2 http-server: 0.12.3 ios-sim: 9.0.0 link-module-alias: 1.2.0 npm: 7.18.1 nvm: 0.0.4 pm2: 3.2.2 react-devtools: 4.10.4 remotedebug-ios-webkit-adapter: 0.4.2 serve: 11.3.0 serverless: 1.44.1 sharp-cli: 1.15.0 spawn-sync: 2.0.0 standard-version: 9.1.1 surge: 0.21.3 try-thread-sleep: 2.0.0 typescript: 4.3.2 update: 0.7.4 wscat: 2.2.1 yarn: 1.22.10 ```

Describe the bug

Querying a link model (i.e. the PostEditor model, given a model Post and a model User with a many-to-many relationship) returns all records, including deleted records. If some items have been deleted and a query is performed:

const data = await DataStore.query(PostEditor)

When DataStore tries to synthesise the data into models locally, it throws an error in the following format:

Unhandled Rejection (Error): Field <field> is required

One workaround for this situation is to manually query the link table using the API package instead:

const result = (await API.graphql(
  graphqlOperation(queries.syncPostEditors)
)) as { data: SyncPostEditorsQuery }
const postEditors =
  result.data.syncPostEditors?.items?.filter(
    (i) => i?._deleted === null
  ) ?? []

Expected behavior

Instead of trying to synthesise data locally for deleted items, DataStore should first filter out the deleted records.

Reproduction steps

  1. Create two models with a many-to-many relationship
  2. Creare some items using the new models
  3. Using DataStore, query the link model
  4. Delete some of the link models
  5. Using DataStore, query the link model again
  6. If DataStore tries to synthesise data locally for deleted items, an error is thrown

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

fistofzen commented 2 years ago

Is there any solution ?

elilambnz commented 2 years ago

@fistofzen I haven't used the DataStore API for 7 months, if you're still having this issue on the latest version then only the workaround in the original issue will prevent this error. One thing to note, if you use the GraphQL API to query data instead, this will not retrieve any offline data.