【问题标题】:UnhandledPromiseRejectionWarning: Error: Network error: apollo_cache_inmemory_1.readQueryFromStore is not a function aws appsync nodejsUnhandledPromiseRejectionWarning:错误:网络错误:apollo_cache_inmemory_1.readQueryFromStore 不是函数 aws appsync nodejs
【发布时间】:2019-03-19 00:05:14
【问题描述】:

我正在尝试通过 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);
    })
});

完整的堆栈跟踪如下:

UnhandledPromiseRejectionWarning:错误:网络错误: apollo_cache_inmemory_1.readQueryFromStore 不是函数 在新的 ApolloError (/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 在 /Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1680:21 在 Array.forEach () 在 /Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1679:22 在 Map.forEach () 在 QueryManager.broadcastQueries (/Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1672:26) 在 /Users/kanhaiagarwal/appsync1/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1175:35 在(节点:23377)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于抛出 在没有 catch 块的异步函数内部,或通过拒绝 未使用 .catch() 处理的承诺。 (拒绝编号:1) (节点:23377)[DEP0018] 弃用警告:未处理的承诺 拒绝被弃用。在未来,承诺拒绝是 未处理将以非零退出终止 Node.js 进程 代码。

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

【问题讨论】:

  • 没有人知道关于带有 nodejs 和 appsync 的 aws lambda 的知识吗?
  • 我遇到了同样的问题,在 Node.js 中运行。你有没有让它发挥作用?
  • 我创建了一个issue in GitHub。看起来我使用的代码是正确的,因为它可以在浏览器中运行。这只是 Node.js 的问题。
  • 仍然没有得到任何解决方案。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2021-03-31
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多