[英]Unable to call AppSync GraphQL api from Lambda: TypeError: Cannot read property 'match' of null
我正在尝试从 Lambda 调用我的 AWS AppSync graphQL API,但我遇到了一个我无法解决的错误,所以我想请你帮忙弄清楚我做错了什么。
凭据似乎是正确的,并且查询应该是正确的(见下文)。
lambda function 是使用无服务器框架部署的,我可以使用sls invoke --function <my_function_name>
调用它。
我一直在关注这篇文章
我得到的错误是:
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "TypeError: Cannot read property 'match' of null",
"trace": [
"Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'match' of null",
" at process.<anonymous> (/var/runtime/index.js:35:15)",
" at process.emit (events.js:310:20)",
" at process.EventEmitter.emit (domain.js:482:12)",
" at processPromiseRejections (internal/process/promises.js:209:33)",
" at processTicksAndRejections (internal/process/task_queues.js:98:32)"
]
}
Error --------------------------------------------------
Error: Invoked function failed
at AwsInvoke.log (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invoke/index.js:101:31)
at AwsInvoke.tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:517:31)
at Promise._settlePromise (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:574:18)
at Promise._settlePromise0 (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:619:10)
at Promise._settlePromises (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:699:18)
at _drainQueueStep (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
at process.topLevelDomainCallback (domain.js:126:23)
这是我的 Lambda function 全文:
'use strict'
const AWS = require('aws-sdk');
const gql = require('graphql-tag');
const AWSAppSyncClient = require('aws-appsync').default;
require('isomorphic-fetch');
require('es6-promise').polyfill();
module.exports.pushNotificationHandler = async (event, context, callback) => {
const appsyncClient = new AWSAppSyncClient({
url: process.env.GRAPHQL_API_ID,
region: process.env.AWS_REGION,
auth: {
type: 'AWS_IAM',
credentials: AWS.config.credentials
},
disableOffline: true
},
{
defaultOptions: {
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
},
}
);
const client = await appsyncClient.hydrated();
try {
const result = await client.query({
query: gql`
query GetUser {
getUser(id: "<A_VALID_USER_ID") {
firstname
}
}
`,
variables: {}
});
console.log(JSON.stringify(result));
return result;
} catch (error) {
console.log(JSON.stringify(error));
return error;
}
callback(null, event);
};
证书
我通过简单地登录并签入CloudWatch
来检查凭据。 有一个访问密钥和一个 session 令牌,所以我假设我的凭据是正确的。
询问
我通过从 AppSync 控制台执行它检查的查询。 它应该工作。
IAM 权限
权限在 Serverless 配置文件中设置,但与以下内容相同:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"appsync:GraphQL"
],
"Resource": [
"arn:aws:appsync:<REGION>:<ACCOUNTID>:apis/<APIID>/*"
]
}
]
}
我究竟做错了什么?
尝试坚持只使用return
而不是callback
。 使用async
function 意味着您不需要使用回调,这可能是影响 Lambda function 响应的原因。
当输入不正确或 appSyncClient 未正确实例化时,通常会发生此错误。 确保
GRAPHQL_API_ID
AWS_REGION
具有合法的 graphQL 端点credentials
存在另外,我建议您将 go 到 AppSync UI 控制台并请求相同的查询,看看它是否返回结果。 如果是这样,那么这是与 appSyncClient 的初始化有关的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.