javascript - 将 AWS Appsync 订阅与 Node JS 和 EJS 结合使用时出现问题

标签 javascript node.js ejs aws-appsync

这是我的第一个问题,我最近才开始编写 Node JS 代码。现在我正在使用 NodeJS 和 EJS 作为我的应用程序的模板引擎。我的数据库是 DynamoDB,我想通过使用 AWS Appsync 实时制作一个表。使用 Appsync,我可以查询和更改表中的字段,但我似乎无法订阅任何更改。当我调用我的订阅时,也没有打开 MQTT websockets。

我尝试按照 Appsync 文档中的 Appsync 订阅示例进行操作,但它似乎不起作用。

注意:我已经定义了我的 infoID 变量,类型信息{}是在模式中创建的,并且 mutate 和 query 都有效。只是订阅不起作用,并且没有在模板上创建 MQTT websocket(这甚至可以使用 NodeJS 和 EJS 吗?)。

我的架构如下:

type Mutation {
	deleteInfo(infoID: String!): information
}

type Subscription {
	onDeleteInfo(infoID: String): information
		@aws_subscribe(mutations: ["deleteInfo"])
}

而我用来查询订阅的代码是这样的:

const query = gql(`
  query($infoID: String!){
          getInformation(infoID: $infoID) {
            infoID
            noOfDays
            noOfItems
            infoName
   }
           
  }`);

  // Set up a subscription query
  const subquery = gql(`
  subscription ($infoID: String) {
  onDeleteInfo(infoID: $infoID) {
    infoID
    noOfDays
    noOfItems
    infoName
  }
  }`);

  const client = new AWSAppSyncClient({
    url: url,
    region: region,
    auth: {
        type: type,
        credentials: credentials,
    }
});

client.hydrated().then(function (client) {
    //Now run a query
    client.query({ query: query, variables: {infoID : infoID} })
        .then(function logData(data) {
            console.log('results of query: ', data);
            var responseData = data;
            res.send(responseData);
        })
        .catch(console.error);

    //Now subscribe to results
    const realtimeResults = function realtimeResults(data) {
            console.log('realtime data: ', data);
            console.log('subcribe is called');
        };

    const observable = client.subscribe({
              query: subquery,
              variables: {infoID : infoID} });

    observable.subscribe({
              next: realtimeResults,
              complete: console.log,
              error: console.log
          });
 };         

我的突变代码是:

const mutation = gql(`
    mutation($infoID: String!){
            deleteInfo(infoID: $infoID) {
              infoID
              noOfDays
              noOfItems
              infoName
             }
    }`);

  const client = new AWSAppSyncClient({
    url: url,
    region: region,
    auth: {
        type: type,
        credentials: credentials,
    }
  });

  client.hydrated().then(function (client) {
      //Now run a query
      client.mutate({ mutation: mutation, variables:{infoID: infoID} })
          .then(function logData(data) {
              console.log('results of mutate: ', data);
          })
          .catch(console.error);
  });

感谢任何以任何方式回答、阅读或提供帮助的人!

最佳答案

如果您可以从您的应用程序进行变更和查询,那么听起来您已成功连接到 AppSync。要调试订阅问题,您需要查看请求的网络响应。如果设置不正确,该服务将发送一条错误消息。

要检查的一个常见问题是 subscription 是否在您的架构中定义。

schema {
    query: Query
    mutation: Mutation
    subscription: Subscription
}

如果是这种情况,您在发出订阅请求时会收到的错误消息是:Schema is not configured for subscriptions.

关于javascript - 将 AWS Appsync 订阅与 Node JS 和 EJS 结合使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49965329/

相关文章:

node.js - 无法在 Virtualbox 上的 package.json 中运行 $(git rev-parse --short HEAD)

node.js - 如何在 meteor 中访问当前模板中的元素?

html - 在数据库中显示html文本发生错误

javascript - node 中 module.exports 之间的区别

javascript - jquery slideDown 和 slideUp() 不适用于部分显示

javascript - 如何将 jQuery "toggle"函数分解为单独的 "show"和 "hide"函数?

javascript - 我想用 javascript 将链接延迟 500

node.js - 表不存在 Sequelize

node.js - JupyterLab plotly 扩展错误 : Cannot find module jupyter\lab\staging\node_modules\ejs\postinstall. js

html - 我用 ejs 修复了一个奇怪的语法错误,我想知道问题是什么