Angular 从 Appsync 客户端订阅 GraphQL 变更

Angular subscription to GraphQL mutation from Appsync Client

我正在 Angular 中使用 GraphQL 和 AWS 中的 Appsync 开发聊天应用程序。创建新聊天时的工作流程是模拟新聊天并将其发送到服务器,在服务器端为此聊天生成 chat_id,我需要检索并将其发送到本地商店。

我的想法是我应该能够订阅 mutate 函数,或者我在执行 mutation 时创建的可观察查询。但是,我在尝试订阅时收到错误 "observable.subscribe is not a function"。如果我尝试订阅整个功能(client.mutate 功能)也是如此。

这是导致问题的当前代码:

this.appsync
      .hc()
      .then(client => {
        const observable: ObservableQuery = client.mutate({
          mutation: createChatRoom,
          variables: {
            input: {
              user_id: user.id,
              text: text,
            }
          },
          fetchPolicy: "no-cache"
        })
        observable.subscribe(data => console.log(data));
      })

我还可以控制台记录 observable 并查看值存储在 observable 对象的 __zone_symbol_value 变量中。但是我无法检索这些值,也无法订阅可观察对象。

关于如何推进这项工作有任何想法或提示吗?

我能够通过在 client.mutate 成为 运行 之后添加一个 .then() 语句来解决这个问题。解决方案代码:

this.appsync
  .hc()
  .then(client => {
    const observable: ObservableQuery = client.mutate({
      mutation: createChatRoom,
      variables: {
        input: {
          user_id: user.id,
          text: text,
        }
      },
      fetchPolicy: "no-cache"
    }).then(data => {
          conversation.id = data.data.createChatRoom.chat_id;
  })

出于某种原因,我不得不打电话给 data.data,我不确定为什么。如果有人知道,请随时回答。否则效果很好 :).