我试图通过nodejs为我的AWS AppSync应用程序调用Graphql查询。我遇到的错误是
UnhandledPromiseRejectionWarning:错误:网络错误: apollo_cache_inmemory_1.readQueryFromStore
这是我的index.js代码
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var config = {
AWS_ACCESS_KEY_ID: <ACCESS_KEY_ID>,
AWS_SECRET_ACCESS_KEY: <SECRET_KEY>,
HOST: '<HOST_URL>',
REGION: 'us-west-2',
PATH: '/graphql',
ENDPOINT: '<AWS_APPSYNC_ENDPOINT>',
};
config.ENDPOINT = "https://" + config.HOST + config.PATH;
exports.default = config;
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 AppSync module
const AUTH_TYPE = "AWS_IAM";
const AWSAppSyncClient = require('aws-appsync').default;
const url = config.ENDPOINT;
const region = config.REGION;
const type = AUTH_TYPE.AWS_IAM;
// If you want to use API key-based auth
const apiKey = 'xxxxxxxxx';
// 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: config.REGION,
credentials: new AWS.Credentials({
accessKeyId: config.AWS_ACCESS_KEY_ID,
secretAccessKey: config.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 {
getSample {
mobileNumber
}
}
`);
// Set up Apollo client
const client = new AWSAppSyncClient({
url: url,
region: region,
auth: {
type: type,
credentials: credentials,
}
});
client.hydrated().then(function a(client) {
client.query({query: query});
client.query({ query: query, fetchPolicy: 'network-only'}).then(function(data) {
console.log("data: " + queryResult);
})
});完整的堆栈跟踪如下:
错误:网络错误:在(/Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:124:32) /Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1248:45 at /Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/的新ApolloError中apollo_cache_inmemory_1.readQueryFromStore不是一个函数阿波罗-client/bundle.umd.js:1680:21在/Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1679:22 at Map.forEach () at Map.forEach () at QueryManager.broadcastQueries (/Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1672:26) at /Users/kanhaiagarwal/appsync1 1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1175:35 at (节点:23377) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。(拒绝id: 1) (节点:23377) DEP0018 DeprecationWarning:未处理的承诺拒绝被取消。在未来,承诺不处理的拒绝将使用非零退出代码终止Node.js进程。
有人能为这件事提出解决方案吗?
发布于 2018-10-26 00:23:21
造成此问题的原因是没有为某些全局对象提供appsync包所依赖的window对象。这在Node.js环境中是不存在的,在脚本开始时添加以下内容应该可以使其工作:
global.window = global.window || {
setTimeout: setTimeout,
clearTimeout: clearTimeout,
WebSocket: global.WebSocket,
ArrayBuffer: global.ArrayBuffer,
addEventListener: function () { },
navigator: { onLine: true }
};下面是提出答案的GitHub问题:
https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/276#issuecomment-432983691
https://stackoverflow.com/questions/52801864
复制相似问题