graphql - AppSync/Amplify - 如何定义 GraphQL 订阅
问题描述
我正在使用 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
}
}
推荐阅读
- javascript - 针对 redis 之类的 3rd 方软件编写测试的方法是什么
- amazon-web-services - 我们可以使用 AWS Lambda 打开现有的 Web 应用程序吗?
- mysql - 如何解决“mysqldump: Error 2013: Lost connection to MySQL server during query when dump table”
- mysql - 如何制作无状态的mysql docker容器
- c# - 使用 benchmarkdotnet 执行性能测试,而不在 .netcore 中生成解决方案(.sln)文件
- c# - 使用 GetPreferredSize() 时如何计算大小?
- python - 如何登录受限站点
- node.js - 如何在节点 js 中对嵌套函数进行单元测试?
- dart - 应用程序启动后如何启动一次登录和启动屏幕页面
- julia - Julia 1.1 x=x+1 in for 循环返回错误