AppSync / Amplify-如何定义GraphQL订阅

时间:2018-11-12 02:49:07

标签: graphql aws-appsync aws-amplify

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

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

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

1 个答案:

答案 0 :(得分:3)

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

尝试一下

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