gpt4 book ai didi

node.js - UnhandledPromiseRejectionWarning : Error: Network error: apollo_cache_inmemory_1. readQueryFromStore 不是函数 aws appsync nodejs

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:23 30 4
gpt4 key购买 nike

我正在尝试通过 nodejs 为我的 AWS AppSync 应用程序调用 Graphql 查询。我遇到的错误是

UnhandledPromiseRejectionWarning: Error: Network error: 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);
})
});

完整的堆栈跟踪如下:

UnhandledPromiseRejectionWarning: Error: Network error: apollo_cache_inmemory_1.readQueryFromStore is not a function at new ApolloError (/Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:124:32) at /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/apollo-client/bundle.umd.js:1680:21 at Array.forEach () at /Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1679:22 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/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1175:35 at (node:23377) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:23377) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

有人可以为此提出解决方案吗?

最佳答案

该问题是由于未提供 aws-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

关于node.js - UnhandledPromiseRejectionWarning : Error: Network error: apollo_cache_inmemory_1. readQueryFromStore 不是函数 aws appsync nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52801864/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com