UnhandledPromiseRejectionWarning:错误:网络错误:apollo_cache_inmemory_1.readQueryFromStore不是aws appsync nodejs函数

时间:2018-10-14 10:49:05

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

我正在尝试通过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()处理的Promise。 (拒绝ID:1)   (节点:23377)[DEP0018]弃用警告:未处理的承诺   不推荐使用。将来,应承诺拒绝   未处理将以非零退出终止Node.js进程   代码。

有人可以为此提出建议吗?

1 个答案:

答案 0 :(得分:0)

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