首页 > 解决方案 > AWS AppSync Javascript 订阅引发 404 错误

问题描述

您好我正在尝试通过此示例使用 JS 订阅 AWS AppSync https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-javascript.html

查询用户表时,我得到的结果很好。但我似乎无法订阅新结果。

我得到的是一个 404 错误:

https://some-iot-url.iot.eu-west-1.amazonaws.com/mqtt?X-Amz-Algorithm=..... 404 (Not Found)

出现以下错误:

错误代码:7

errorMessage:“AMQJS0007E 套接字错误:未定义。”

调用上下文:未定义

IOT url 的奇怪之处在于它与我在 IoT 核心仪表板中的 IOT url 不匹配。这是预期的吗?

更多信息:我将代码与 webpack 捆绑在一起(但我想这与错误无关)

这是我的完整代码

配置.js

Object.defineProperty(exports, "__esModule", { value: true });
var config = {
    AWS_ACCESS_KEY_ID: '',
    AWS_SECRET_ACCESS_KEY: '',
    HOST: 'my-host.appsync-api.eu-west-1.amazonaws.com',
    REGION: 'eu-west-1',
    PATH: '/graphql',
    ENDPOINT: '',
};
config.ENDPOINT = "https://" + config.HOST + config.PATH;
exports.default = config;

应用程序.js

/**
* This shows how to use standard Apollo client on Node.js
*/

global.WebSocket = require('ws');
global.window = global.window || {
    setTimeout: setTimeout,
    clearTimeout: clearTimeout,
    WebSocket: global.WebSocket,
    ArrayBuffer: global.ArrayBuffer,
    addEventListener: function () { },
    navigator: { onLine: true }
};
global.localStorage = {
    store: {},
    getItem: function (key) {
        return this.store[key]
    },
    setItem: function (key, value) {
        this.store[key] = value
    },
    removeItem: function (key) {
        delete this.store[key]
    }
};
require('es6-promise').polyfill();
require('isomorphic-fetch');

// Require exports file with endpoint and auth info
const aws_exports = require('./aws-exports').default;

// Require AppSync module
const AUTH_TYPE = require('aws-appsync/lib/link/auth-link').AUTH_TYPE;
const AWSAppSyncClient = require('aws-appsync').default;

const url = aws_exports.ENDPOINT;
const region = aws_exports.REGION;
const type = AUTH_TYPE.API_KEY;

// If you want to use API key-based auth
const apiKey = 'my-api-key';
// If you want to use a jwtToken from Amazon Cognito identity:
const jwtToken = 'xxxxxxxx';

// // If you want to use AWS...
// const AWS = require('aws-sdk');
// AWS.config.update({
//     region: aws_exports.REGION,
//     credentials: new AWS.Credentials({
//         accessKeyId: aws_exports.AWS_ACCESS_KEY_ID,
//         secretAccessKey: aws_exports.AWS_SECRET_ACCESS_KEY
//     })
// });
// const credentials = AWS.config.credentials;

// Import gql helper and craft a GraphQL query
const gql = require('graphql-tag');
const query = gql(`
query AllUser {
listUsers(first: 20) {
    __typename
        items{
        id
        userId
        username
    }
}
}`);

// Set up a subscription query
const subquery = gql(`
subscription NewUser {
subscribeToNewUsers {
    __typename
    id
    userId
    username
}
}`);

// Set up Apollo client
const client = new AWSAppSyncClient({
    url: url,
    region: region,
    auth: {
        type: type,
        apiKey: apiKey,
    }
});

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

    //Now subscribe to results
    const observable = client.subscribe({ query: subquery });
    console.log(observable)
    const realtimeResults = function realtimeResults(data) {
        console.log('realtime data: ', data);
    };

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

标签: javascriptamazon-web-servicesaws-sdkaws-appsync

解决方案


嘿抱歉回复慢。如果您在浏览器上下文中运行它,您可以尝试注释掉以下代码行:

/*
global.WebSocket = require('ws');
global.window = global.window || {
    setTimeout: setTimeout,
    clearTimeout: clearTimeout,
    WebSocket: global.WebSocket,
    ArrayBuffer: global.ArrayBuffer,
    addEventListener: function () { },
    navigator: { onLine: true }
};
global.localStorage = {
    store: {},
    getItem: function (key) {
        return this.store[key]
    },
    setItem: function (key, value) {
        this.store[key] = value
    },
    removeItem: function (key) {
        delete this.store[key]
    }
};
require('isomorphic-fetch');
*/

// You should be able to keep this.
require('es6-promise').polyfill();

这个示例有一些变通方法可以使它与节点很好地工作,但它的当前状态似乎存在一些问题。让我知道这是否有效。谢谢!


推荐阅读