-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Comments
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 |
As a workaround, instead of using data returned by the subscription, I call a classic DataStore.query when new data are emitted.
|
I also see this issue and the same problem arises after updating the model. The re-query workaround addresses both issues. |
I am also have this issue, I'll try the re-query workaround. |
@sebboe This should be fixed. Are you still seeing this problem on the latest version of amplify? |
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) |
It is working for me, 1 level deep.
type Post @model {
id: ID!
title: String!
rating: Int!
status: PostStatus!
# New field with @hasmany
comments: [Comment] @hasmany(indexName: "byPost", fields: ["id"])
}
# New model
type Comment @model {
id: ID!
postID: ID! @index(name: "byPost", sortKeyFields: ["content"])
post: Post! @belongsTo(fields: ["postID"])
content: String!
}
If Comment.postID does not contain a value that exists in the Post collection, then the Comment.post field will not show up in the results. Note that Post.comments never shows up in results for me. If I want to query for the relationship, I have to query and reduce the Comments collection like:
const [postComments, setPostComments] = React.useState({ });
const subscription = DataStore.observeQuery(Comments).subscribe(({ items }) => {
setPostComments(prev => items.reduce((a, o) => {
if (o.postID in a) {
a[o.postID][o.id <http://o.id/>] = o;
} else {
a[o.postID] = { [o.id]: o }
}
return a;
}, { …prev }));
});
Then the comment array for a post like: const comments = Object.values(postComments[postId]);
… On Aug 17, 2022, at 7:59 PM, Colin Birkenstock ***@***.***> wrote:
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)
—
Reply to this email directly, view it on GitHub <#9682 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAH6EIRJFBD4ZV6JLQF3EIDVZWDITANCNFSM5QJ5SISQ>.
You are receiving this because you commented.
|
May be related to #10211 |
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. |
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. |
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 |
@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 |
Before opening, please confirm:
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:
Expected behavior
I would like to directly return the nested object.
Reproduction steps
GraphQL schema(s)
Log output
Additional information
No response
The text was updated successfully, but these errors were encountered: