代码之家  ›  专栏  ›  技术社区  ›  kkesley

AWS AppSync javascript订阅引发404错误

  •  0
  • kkesley  · 技术社区  · 8 年前

    嗨,我正在尝试订阅使用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

    错误消息:“amqjs0007e套接字错误:未定义。”

    InvocationContext:未定义

    关于物联网URL的奇怪之处在于它与我在物联网核心仪表板中的物联网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;
    

    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,
        });
    });
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   mparis    7 年前

    嘿,抱歉反应慢了。如果您在浏览器上下文中运行此命令,可以尝试注释以下代码行:

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

    此示例有一些解决方法,可以使其与节点很好地工作,但在其当前状态下似乎存在一些问题。如果可行,请告诉我。谢谢!