您好我正在尝试使用此示例使用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)
出现以下错误:
的errorCode:7
errorMessage:“AMQJS0007E套接字错误:未定义。”
invocationContext:未定义
IOT网址的奇怪之处在于它与物联网核心仪表板中的IOT网址不匹配。这是预期的吗?
更多信息:我将代码与webpack捆绑在一起(但我猜这与错误无关)
这是我的完整代码
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;
/**
* 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,
});
});
答案 0 :(得分:0)
抱歉,响应缓慢。如果您是在浏览器上下文中运行此程序,可以尝试注释掉以下代码行:
/*
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();
此示例具有一些变通方法,使其可以与node很好地配合使用,但是当前状态似乎存在一些问题。让我知道是否可行。谢谢!