[英]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.