AWS AppSync Javascript 订阅引发 404 错误

AWS AppSync Javascript Subscription throws 404 Error

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

查询用户table时,我得到的结果很好。但是我好像无法订阅新结果。

我得到的是 404 错误:

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

出现以下错误:

errorCode:7

errorMessage:"AMQJS0007E Socket error:undefined."

invocationContext:undefined

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

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

这是我的完整代码

config.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;

app.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,
    });
});

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

/*
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();

此示例有一些变通办法可以使其与节点很好地配合使用,但它的当前状态似乎存在一些问题。如果可行,请告诉我。谢谢!