【问题标题】:AppSync/Amplify - how to define GraphQL subscriptionsAppSync/Amplify - 如何定义 GraphQL 订阅
【发布时间】:2019-04-14 18:34:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: graphql aws-appsync aws-amplify


    【解决方案1】:

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

    试试这个

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

    【讨论】:

    • 感谢您的回答,效果很好。我想没有办法直接在author 字段中使用id 进行订阅,而是我们必须添加一个额外的参数authorId?
    • 我认为mutations: "createMessage" 应该是mutations: ["createMessage"]。没有括号,我无法让它工作。
    猜你喜欢
    • 2021-03-11
    • 2020-12-28
    • 2018-10-20
    • 2020-03-05
    • 2021-01-29
    • 2022-08-17
    • 2019-12-03
    • 2020-10-27
    • 2019-08-09
    相关资源
    最近更新 更多