You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nested model is replaced with function after it is delivered by observeQuery or onUpdate.
Selection Set setting is not respected by observeQuery or onUpdate.
Expected behavior
Selection Set setting will work the same for observeQuery as it does for other functions i.e. get, or list.
// components/TodoList.tsx"use client"importtype{Schema}from"@/amplify/data/resource"importtype{SelectionSet}from"aws-amplify/data"import{generateClient}from"aws-amplify/data"import{useEffect,useState}from"react"// generate your data client using the Schema from your backendconstclient=generateClient<Schema>()constselectionSet=["id","title","content.*"]asconsttypeTodoModel=SelectionSet<Schema["Todo"],typeofselectionSet>exportdefaultfunctionTodoList(){const[todos,setTodos]=useState<TodoModel[]>([])asyncfunctionlistTodos(){// fetch all todosconst{ data }=awaitclient.models.Todo.list({selectionSet: selectionSet,})console.log("After Page Load",data)setTodos(data)}useEffect(()=>{listTodos()},[])useEffect(()=>{constsub=client.models.Todo.observeQuery({selectionSet: selectionSet,}).subscribe(({ items })=>{console.log("After Adding New Item",items)setTodos([...items])})return()=>sub.unsubscribe()},[])return(<div><h1>Todos</h1><buttononClick={async()=>{const{data: newTodo}=awaitclient.models.Todo.create({title: "Title",})awaitclient.models.Content.create({text: "Content",todoId: newTodo.id,})}}>
Create
</button><ul>{todos.map((todo)=>(<likey={todo.id}>{todo.title} - {todo.content.text}</li>))}</ul></div>)}
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
The text was updated successfully, but these errors were encountered:
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.
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.
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.
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 by
observeQuery
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
The text was updated successfully, but these errors were encountered: