【问题标题】:Angular subscription to GraphQL mutation from Appsync ClientAppsync 客户端对 GraphQL 突变的 Angular 订阅
【发布时间】:2020-03-21 14:52:44
【问题描述】:

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

我的想法是我应该能够订阅 mutate 函数,或者我在执行突变时创建的可观察查询。但是,我在尝试订阅时收到错误“observable.subscribe 不是函数”。如果我尝试订阅整个函数(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 变量中。但我无法检索这些值,或订阅 observable。

关于如何推进这件事的任何想法或提示?

【问题讨论】:

    标签: angular graphql apollo-client aws-appsync


    【解决方案1】:

    我可以通过在运行 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,但我不知道为什么。如果有人知道,请随时回答。否则效果很好:)。

    【讨论】:

      猜你喜欢
      • 2020-08-31
      • 2021-07-28
      • 2023-04-03
      • 2022-12-12
      • 2023-01-31
      • 2019-12-27
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多