AppSync/Amplify - 如何定义 GraphQL 订阅

AppSync/Amplify - how to define GraphQL subscriptions

我正在使用 Amplify 自动生成查询、变更和订阅。我有这种类型:

type Message @model {  
  id: ID!
  author: User @connection(name: "UserMessages")
  content: String
}

如何使用 Amplify 生成的架构将 authorID 添加为新消息订阅的过滤器?

您可以根据需要添加自己的参数化订阅字段。

试试这个

# Add the authorId explicitly and turn off the generated subscriptions.
type Message @model(subscriptions: null) {  
  id: ID!
  author: User @connection(name: "UserMessages")
  authorId: String
  content: String
}
type Subscription {
  onCreateMessage(authorId: String!): Message @aws_subscribe(mutations: "createMessage")
}

订阅的客户端只会收到订阅查询中提供的 authorId 的消息:

subscription SubscribeToNewMessages($id: String!) {
  onCreateMessage(authorId: $id) {
    id
    authorId
    content
  }
}