Open m9rc1n opened 2 months ago
Hi @m9rc1n it looks like you are using observeQuery
on the Todo
model. Although you are creating a Todo
and Content
record subsequently, the initial creation of a Todo record will trigger the subscription but the data it receives does not yet contain the relationship. The creation of the Content
record will not trigger the subscription either.
So, the function in place of the new record's content
field is actually a promise that can be awaited to lazy load the related Content
data.
Hi @chrisbonifacio,
Thanks for looking into this issue.
I see your point. On the other hand, this design decision might cause confusion among end users (i.e. how to use nested fields in subscriptions?) and lead to some race conditions.
For instance, if I do:
useEffect(() => {
const sub = client.models.Todo.observeQuery({
selectionSet: selectionSet,
}).subscribe(async ({ items }) => {
console.log("After Adding New Item", items)
for (const item of items) {
if (typeof item.content == "function") {
const { data: content} = await item.content()
console.log("Updated content" , content)
}
}
setTodos([...items])
})
return () => sub.unsubscribe()
}, [])
Then the content will be still undefined
as subscription Todo
arrives faster than Content
update is completed.
Also I was able to recreate this issue using reverse subscription - listening to updates on Content
instead of Todo
. In this case Todo is created first (awaited) and todoId is being passed as argument to Content, so that race condition shouldn't be a problem.
Hi @m9rc1n we're going to classify this as a bug at the moment, as this probably needs some discussion. Returning the relationship before it exists won't be possible via the subscription so we will look into improving this experience, at least avoiding confusion around related data in messages from onCreate subscriptions.
Hi @chrisbonifacio, sure thing. Let me know if you need some testing or help investigating / discussing over this issue.
It is worth noting that typescript errors pop up if an onCreate or onUpdate subscription uses a selection set and we try to lazy load the relations because they're not typed as functions.
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
GraphQL API, DataStore
Amplify Version
v6
Amplify Categories
api
Backend
Amplify Gen 2 (Preview)
Environment information
Describe the bug
Nested model is replaced with function after it is delivered by
observeQuery
oronUpdate
. Selection Set setting is not respected byobserveQuery
oronUpdate
.Expected behavior
Selection Set setting will work the same for observeQuery as it does for other functions i.e. get, or list.
Reproduction steps
Code Snippet
Log output
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