我们如何通过查询获得用户对话。理想的做法是查询用户表并加载所有用户对话。例子:
final user = await Amplify.DataStore.query(User.classType, where: User.ID.eq(id))
user.conversations 加载对话列表。
如果无法做到这一点,接下来将是获取具有特定用户的所有对话。示例不工作:
await Amplify.DataStore.query(UserConversation.classType
where: UserConversation.USER.eq(user.id)
)
type Message @model @auth(rules: [{allow: public}]) {
id: ID!
ConversationId: Conversation @connection
UserId: User @connection
body: String
createdDate: AWSDateTime!
}
type Conversation @model @auth(rules: [{allow: public}]) {
id: ID!
date: AWSDateTime
readBy: [ReadBy] @connection(keyName: "byConversation", fields: ["id"])
users: [UserConversation] @connection(keyName: "byConversation", fields: ["id"])
name: String
image: String
}
type ReadBy @model @auth(rules: [{allow: public}]) @key(name: "byConversation", fields: ["conversationID"]) {
id: ID!
date: AWSDateTime
RedByUsers: User @connection
conversationID: ID
}
type User @model @auth(rules: [{allow: public}]) {
id: ID!
firstName: String
lastName: String
userImg: String
createdDate: AWSDateTime
updatedDate: AWSDateTime
email: AWSEmail
conversations: [UserConversation] @connection(keyName: "byUser", fields: ["id"])
}
type UserConversation @model(queries: null) @key(name: "byUser", fields: ["userID", "conversationID"]) @key(name: "byConversation", fields: ["conversationID", "userID"]) @auth(rules: [{allow: public}, {allow: public}]) {
id: ID!
userID: ID!
conversationID: ID!
user: User! @connection(fields: ["userID"])
conversation: Conversation! @connection(fields: ["conversationID"])
}