Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastore - Observe query results in real-time with @hasOne relation doesn't add nested data after first sync #9682

Closed
5 tasks done
sebboe opened this issue Mar 7, 2022 · 13 comments
Assignees
Labels
DataStore Related to DataStore category transferred This issue was transferred from another Amplify project

Comments

@sebboe
Copy link

sebboe commented Mar 7, 2022

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

No response

Amplify CLI Version

7.6.22

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

I'm facing an issue while observing objects with "@hasone" relationship. When adding a new object, the observer returns only the main object, without the relation field. After one refresh/moving to another component and coming back, the nested object appears as part of the main object.

According to the doc:

observeQuery(...) returns an initial data set, similar to query(...), and also automatically subscribes to subsequent changes to the query.

Expected behavior

I would like to directly return the nested object.

Reproduction steps

  1. Link two models with "@hasone"
  2. Add object with relation
  3. Observe objects:
    this.flightList = DataStore.observeQuery(Flight).subscribe(snapshot => {
      const { items, isSynced } = snapshot;
      console.log(`[Snapshot] item count: ${items.length}, isSynced: ${isSynced}`);
      console.log(items);
      this.flights = items;
    });

GraphQL schema(s)

# Put schemas below this line
type Flight @model {
  id: ID!
  pilot: String!
  airplane: Aircraft @hasOne(fields: ["airplaneId"])
  airplaneId: ID
}

type Aircraft @model {
  id: ID!
  registration: String!
  category: String
  lastIndex: String
  specs: AircraftSpecs
}

type AircraftSpecs {
  passenger:Boolean
  fuel:String
}

Log output

# Object returned right after saving it:
{
    "id": "5a91ab15-0866-4919-9703-3083018cdeab",
    "pilot": "Sébastien",
    "airplaneId": "3e154d19-af70-47d2-9d66-829fce65b93a",
    "_version": 1,
    "_lastChangedAt": 1646679807385,
    "_deleted": null
}
# Object returned after refresh
{
    "id": "5a91ab15-0866-4919-9703-3083018cdeab",
    "pilot": "Sébastien",
    "airplaneId": "3e154d19-af70-47d2-9d66-829fce65b93a",
    "_version": 1,
    "_lastChangedAt": 1646679807385,
    "_deleted": null,
    "airplane": {
        "id": "3e154d19-af70-47d2-9d66-829fce65b93a",
        "registration": "AV-ION",
        "category": "Airplane",
        "lastIndex": "22299:99",
        "createdAt": "2022-03-06T23:30:51.363Z",
        "updatedAt": "2022-03-07T19:03:27.671Z",
        "specs": {
            "passenger": true,
            "fuel": "AVGAS"
        },
        "_version": 8,
        "_lastChangedAt": 1646679807692,
        "_deleted": null
    }
}

Additional information

No response

@sebboe sebboe changed the title Datastore object with @hasOne relation sync after component refresh only Datastore object with @hasOne sync relation after component refresh only Mar 7, 2022
@sebboe sebboe changed the title Datastore object with @hasOne sync relation after component refresh only Datastore - Observe query results in real-time with @hasOne relation doesn't add nested data after first sync Mar 8, 2022
@josefaidt
Copy link
Contributor

Hey @sebboe 👋 thanks for raising this! I'm going to transfer this over to the amplify-js repo for better support as this appears to be an issue with DataStore

@josefaidt josefaidt added the transferred This issue was transferred from another Amplify project label Mar 9, 2022
@josefaidt josefaidt transferred this issue from aws-amplify/amplify-cli Mar 9, 2022
@chrisbonifacio chrisbonifacio self-assigned this Mar 9, 2022
@chrisbonifacio chrisbonifacio added DataStore Related to DataStore category pending-triage Issue is pending triage labels Mar 9, 2022
@sebboe
Copy link
Author

sebboe commented Mar 30, 2022

As a workaround, instead of using data returned by the subscription, I call a classic DataStore.query when new data are emitted.

    this.flightList = DataStore.observeQuery(Flight).subscribe(snapshot => {
      //const { items, isSynced } = snapshot;
      //console.log(`[Snapshot] item count: ${items.length}, isSynced: ${isSynced}`);
      this.refreshData().then(flights => {
        this.flights = flights;
        console.log(this.flights);
      })
    });
  }

  async refreshData(){
    this.flights = await DataStore.query(Flight);
    return this.flights
  }

@chow11
Copy link

chow11 commented Apr 19, 2022

I also see this issue and the same problem arises after updating the model. The re-query workaround addresses both issues.

@tomyates
Copy link

tomyates commented May 5, 2022

I am also have this issue, I'll try the re-query workaround.

@svidgen
Copy link
Member

svidgen commented Jun 29, 2022

@sebboe This should be fixed. Are you still seeing this problem on the latest version of amplify?

@svidgen svidgen added pending-close-response-required and removed good first issue Good for newcomers pending-triage Issue is pending triage labels Jun 29, 2022
@dpilch dpilch closed this as completed Aug 9, 2022
@cbirkenstock
Copy link

I am still not receiving nested objects from the observe part of observeQuery and my datastore version is 3.12.6 (most up to date)

@chow11
Copy link

chow11 commented Aug 18, 2022 via email

@dpilch dpilch reopened this Aug 18, 2022
@dpilch
Copy link
Member

dpilch commented Aug 18, 2022

May be related to #10211

@dpilch dpilch added pending-triage Issue is pending triage and removed pending-close-response-required labels Aug 18, 2022
@bc4253
Copy link

bc4253 commented Aug 18, 2022

One thing about updating relationships as being different from adding a new one. In the above example, if postID is null then updating the Comments document only requires setting postID. If postID is already pointing to post "A", then updating just postID to the ID of post "B" fails. Update (and delete - set posID=null) of an existing relationship requires also setting post to the "B" post model.

Why is setting the model reference field required? post is a calculated field.

@bc4253
Copy link

bc4253 commented Aug 18, 2022

I have also noticed that one can set postID to anything if postID was null previously. If postID is a value that is not the ID of a Post document, then no error is generated on the update. querying/observing will obviously not return a Post model in the post field in this case.

@dpilch
Copy link
Member

dpilch commented Feb 20, 2023

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.

@dpilch dpilch closed this as completed Feb 20, 2023
@qwertylolman
Copy link

qwertylolman commented Apr 20, 2024

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.

I'm having exactly the same issue, but with amplify-flutter

@chrisbonifacio
Copy link
Member

chrisbonifacio commented Apr 20, 2024

@qwertylolman can you please create a new issue on the amplify flutter repo with either a link to a sample repo that reproduces the issue or reproduction steps?

You can reference/link to this issue as well to include as context for the amplify flutter team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DataStore Related to DataStore category transferred This issue was transferred from another Amplify project
Projects
None yet
Development

No branches or pull requests