首页 > 解决方案 > AppSync/Amplify - 如何定义 GraphQL 订阅

问题描述

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

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

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

标签: graphqlaws-appsyncaws-amplify

解决方案


您可以添加自己的订阅字段,这些字段根据您的喜好进行参数化。

试试这个

# 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
  }
}

推荐阅读