aws-amplify/amplify-js

DataStore.observequery does not return nested belongsTo fields, but DataStore.query does.

AlexChaseJones opened this issue · 4 comments

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

DataStore

Amplify Categories

api

Environment information

# Put output below this line
  System:
    OS: macOS 12.2.1
    CPU: (10) x64 Apple M1 Pro
    Memory: 38.94 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v14.19.0/bin/yarn
    npm: 6.14.16 - ~/.nvm/versions/node/v14.19.0/bin/npm
    Watchman: 2022.07.04.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 104.0.5112.79
    Safari: 15.3
  npmPackages:
    @babel/core: ^7.12.9 => 7.18.10 
    @react-native-async-storage/async-storage: ^1.17.7 => 1.17.8 
    @react-native-community/netinfo: ^9.3.0 => 9.3.0 
    @react-navigation/bottom-tabs: ^6.3.2 => 6.3.2 
    @react-navigation/drawer: ^6.4.3 => 6.4.3 
    @react-navigation/material-top-tabs: ^6.2.2 => 6.2.2 
    @react-navigation/native: ^6.0.11 => 6.0.11 
    @react-navigation/stack: ^6.2.2 => 6.2.2 
    HelloWorld:  0.0.1 
    aws-amplify: ^4.3.30 => 4.3.30 
    axios: ^0.27.2 => 0.27.2 (0.26.0)
    example:  0.0.1 
    expo: ^45.0.8 => 45.0.8 
    expo-image-manipulator: ~10.3.1 => 10.3.1 
    expo-image-picker: ~13.1.1 => 13.1.1 
    expo-image-picker-multiple: git+https://github.com/NickRance/expo-image-picker-multiple.git => 1.4.1 
    expo-media-library: ^14.2.0 => 14.2.0 
    expo-status-bar: ~1.3.0 => 1.3.0 
    expo-updates: ^0.13.4 => 0.13.4 
    hermes-inspector-msggen:  1.0.0 
    i: ^0.3.7 => 0.3.7 
    ini: ^1.3.5 => 1.3.8 
    inquirer: ^6.5.1 => 6.5.2 
    react: ^17.0.2 => 17.0.2 
    react-dom: 17.0.2 => 17.0.2 
    react-native: ^0.68.2 => 0.68.3 
    react-native-collapsible-tab-view: ^4.5.2 => 4.5.2 
    react-native-gesture-handler: ~2.2.1 => 2.2.1 
    react-native-image-viewing: ^0.2.2 => 0.2.2 
    react-native-loading-spinner-overlay: ^3.0.1 => 3.0.1 
    react-native-pager-view: ^5.4.25 => 5.4.25 
    react-native-radio-buttons-group: ^2.2.11 => 2.2.11 
    react-native-reanimated: ~2.8.0 => 2.8.0 
    react-native-safe-area-context: ^4.3.1 => 4.3.1 
    react-native-search-filter: ^0.1.5 => 0.1.5 
    react-native-snap-carousel: ^3.9.1 => 3.9.1 
    react-native-swipe-gestures: ^1.0.5 => 1.0.5 
    react-native-tab-view: ^3.1.1 => 3.1.1 
    react-native-web: 0.17.7 => 0.17.7 
    typescript: ^4.7.4 => 4.7.4 
  npmGlobalPackages:
    bspeasy: 2.2.3
    corepack: 0.10.0
    expo-cli: 5.4.12
    fony: 1.1.0
    node-gyp: 9.0.0
    nodemon: 2.0.19
    npm: 6.14.16
    serverless: 3.16.0
    webpack-cli: 4.9.2
    webpack: 5.70.0
    yarn: 1.22.19

Describe the bug

DataStore.observequery is not returning belongsto items.

Expected behavior

I expect the belongsto (message) field to be populated in the observequery response, as it is in the query response.

Reproduction steps

My (relevant) models look like this:

type Message @model @auth(rules: [{allow: public}]) {
  id: ID!
  subject: String!
  content: String!
  contentType: String!
  MessageRecipients: [MessageRecipient] @hasMany(indexName: "byMessage", fields: ["id"])
}

type MessageRecipient @model @auth(rules: [{allow: public}]) {
  id: ID!
  isRead: Boolean!
  usergroupID: ID @index(name: "byUserGroup")
  messageID: ID! @index(name: "byMessage")
  message: Message @belongsTo(fields: ["messageID"])
}

If i run this query

DataStore.query(
  MessageRecipient,
  m => m.usergroupID('eq', loggedInUserGroupId)
).then((items) => {
  console.log('query results: ', items)
})

I get this response

query results:  Array [
  MessageRecipient {
    "_deleted": null,
    "_lastChangedAt": 1660588574065,
    "_version": 1,
    "createdAt": "2022-08-15T18:36:14.035Z",
    "id": "c528324c-17f2-469d-9d8c-bee4c7e59272",
    "isRead": false,
    "message": Message {
      "_deleted": null,
      "_lastChangedAt": 1660588573834,
      "_version": 1,
      "content": "test content",
      "contentType": "text",
      "createdAt": "2022-08-15T18:36:13.811Z",
      "id": "70db748f-2ba6-420a-a996-d9c381f82b45",
      "subject": "-",
      "updatedAt": "2022-08-15T18:36:13.811Z",
    },
    "updatedAt": "2022-08-15T18:36:14.035Z",
    "usergroupID": "08a6e978-9946-4b6e-83d2-b4d975fbcd3e",
  }
]

but with observequery: (same filter, same model)

DataStore.observeQuery(
  MessageRecipient,
  m => m.usergroupID('eq', loggedInUserGroupId)
).subscribe(({items}) => {
  console.log('subscribe results', items) 
});

I get this response:

subscribe results: Array [
    MessageRecipient {
      "_deleted": null,
      "_lastChangedAt": 1660588574065,
      "_version": 1,
      "createdAt": "2022-08-15T18:36:14.035Z",
      "id": "c528324c-17f2-469d-9d8c-bee4c7e59272",
      "isRead": false,
      "messageID": "70db748f-2ba6-420a-a996-d9c381f82b45",
      "updatedAt": "2022-08-15T18:36:14.035Z",
      "usergroupID": "08a6e978-9946-4b6e-83d2-b4d975fbcd3e",
    }
]

Am i doing something wrong? or is this expected behavior?

oddly, if I query first then run observequery, the response has the belongsto messages. But any subsequent messageRecipients created that trigger the observequery listener do not have the belongsto message reference.

It seems that observequery for some reason doesn't have a reference to the message in the messageID field. But I save the Message (using DataStore.save) before creating the MessageRecipient object:

const messageObj = await DataStore.save(
  new Message(message)
);

await DataStore.save(
  new MessageRecipient({
    isRead: false,
    message: messageObj,
    messageID: messageObj.id,
    usergroupID
  })
)

So i'm not sure why it wouldn't

this other issue seems related, but it was for amplify-flutter and seems to be resolved: aws-amplify/amplify-flutter#943. and one for amplify-android: aws-amplify/amplify-android#1566

Possibly related: #9682

I'm not able to reproduce this. It may have been incidentally fixed with the lazy loading change. Please comment if you are still experiencing this issue.