我正在尝试使用NodeJS客户端在AWS AppSync上运行GraphQL查询。我使用Amazon's guide入门。
我的代码大致如下:
global.WebSocket = require('ws');
global.window = global.window || {
setTimeout: setTimeout,
clearTimeout: clearTimeout,
WebSocket: global.WebSocket,
ArrayBuffer: global.ArrayBuffer,
addEventListener: function () { },
navigator: { onLine: true }
};
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');
const AWSAppSyncClient = require('aws-appsync').default;
const AUTH_TYPE = require('aws-appsync/lib/link/auth-link').AUTH_TYPE;
const gql = require('graphql-tag');
const handleError = (err) => {
// console.error(err);
throw err;
}
const myFunc = (inputValue) => {
const query = gql(`
// query text in here (works on the AppSync UI)
`);
const client = new AWSAppSyncClient({
url: url,
region: region,
auth: {
type: AUTH_TYPE.API_KEY,
apiKey: apiKey
},
disableOffline: true
});
client.hydrated().then((client) => {
client.query({ query: query, fetchPolicy: 'network-only' })
.then(res => {
console.log("res: " + res);
// some checks on res, then return either true or false
})
.catch(handleError);
});
}
async function main() {
let res;
try {
res = await hasDeliveryOptions("inputValue");
} catch (err) {
res = false;
} finally {
console.log("res: " + res);
}
}
main();
我很难从捕获中返回任何价值。我最终得到“ res:undefined”。
我对NodeJS还是比较陌生,所以也许我将它设置为完全错误的。有人可以指出我需要做些什么才能能够从捕获中返回一个值?