无法从 Lambda 调用 AppSync GraphQL api:TypeError:无法读取 null 的属性“匹配”
Posted
技术标签:
【中文标题】无法从 Lambda 调用 AppSync GraphQL api:TypeError:无法读取 null 的属性“匹配”【英文标题】:Unable to call AppSync GraphQL api from Lambda: TypeError: Cannot read property 'match' of null 【发布时间】:2020-10-20 12:36:36 【问题描述】:我正在尝试从 Lambda 调用我的 AWS AppSync graphQL API,但我遇到了一个我无法解决的错误,所以我想请你帮忙找出我做错了什么。
凭据似乎正确,查询应该正确(见下文)。
lambda 函数是使用无服务器框架部署的,我可以使用sls invoke --function <my_function_name>
调用它。
我一直在关注this article
我得到的错误是:
"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 函数:
'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
来检查凭据。有一个访问密钥和一个会话令牌,所以我会假设我的凭据是正确的。
查询
我通过从 AppSync 控制台执行它检查的查询。它应该可以工作。
IAM 权限
权限在Serverless配置文件中设置,但与以下相同:
"Version": "2012-10-17",
"Statement": [
"Effect": "Allow",
"Action": [
"appsync:GraphQL"
],
"Resource": [
"arn:aws:appsync:<REGION>:<ACCOUNTID>:apis/<APIID>/*"
]
]
我做错了什么?
【问题讨论】:
【参考方案1】:尝试只使用return
而不是callback
。使用 async
函数意味着您不需要使用回调,这可能是影响 Lambda 函数响应的原因。
【讨论】:
【参考方案2】:当输入不正确或 appSyncClient 未正确实例化时,通常会发生此错误。确保
GRAPHQL_API_ID
AWS_REGION
具有合法的 graphQL 端点
credentials
存在
另外,我建议您转到 AppSync UI 控制台并请求相同的 Query 并查看它是否返回结果。如果有,那就是appSyncClient初始化的问题
【讨论】:
以上是关于无法从 Lambda 调用 AppSync GraphQL api:TypeError:无法读取 null 的属性“匹配”的主要内容,如果未能解决你的问题,请参考以下文章
从 Lambda 调用 AWS AppSync graphql API
让 DynamoDB Stream Lambda 函数调用 AWS AppSync 突变
AWS Lambda 函数无法访问 AppSync GraphQL API - 权限被拒绝