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

标签 node.js amazon-web-services aws-appsync

我正在尝试通过 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/

相关文章:

amazon-web-services - 全局二级索引上的 AppSync 查询

amazon-web-services - 如何使用 AppSync Resolvers 和 Aurora 将可选字段插入为空?

ios - AWS AppSync 订阅 iOS 的特定设备/用户?

node.js - 如何保持/*!与 UglifyJS2 的评论?

node.js - 无法在 Google OAuth2.0 中对服务器到服务器应用程序进行身份验证

amazon-web-services - 有没有办法在通过cdk制作RDS时设置默认密码

amazon-web-services - 我可以垂直扩展 Amazon 实例吗?

node.js - 是否可以让服务器运行 node.js 以在运行时安装和运行模块?

javascript - 在discord.js 中添加日期时间描述

amazon-web-services - 如何计算 CloudWatch 日志中 IP 地址的出现次数