我的应用程序在 serverless
框架中,我使用 vtl
模板作为 lambda 解析器
。我的应用程序堆栈是 AppSync
、Lambda
on Node JS
运行时、Serverless
框架和 vtl
模板。
我正在尝试弄清楚如何从我的 lambda 向客户端添加自定义 response headers
并且非常感谢任何输入。到目前为止,请在下面找到我的代码:
lambda
const securityHeaders = {
"content-type": "application/json; charset=utf-8",
"x-content-type-options": "nosniff",
"cache-control": "no-store, no-cache, must-revalidate, proxy-revalidate",
};
callback(null, {
statusCode: 200,
headers: securityHeaders,
body: JSON.stringify({
data,
})
});
return;
无服务器 yml
functions:
getData:
handler: src/handler.getData
events:
- http:
path: getData
method: post
custom:
configValidationMode: off
appSync:
schema: ['graphql-schemas/data.graphql']
authenticationType: AMAZON_COGNITO_USER_POOLS
mappingTemplates:
- dataSource: GetData
type: Query
field: getData
request: "data-request.vtl"
response: "data-response.vtl"
data-response.vtl
## return the body
#if($ctx.result.statusCode == 200)
##if response is 200
$ctx.result.body
#else
##if response is not 200, append the response to error block.
$utils.appendError($ctx.result.body, "$ctx.result.statusCode")
#end
上面的代码在 postman 中给了我结果,但我无法在响应部分看到我的自定义 header 。我想我不知道如何在响应 vtl 中包含 header 。
最佳答案
已经晚了,但我仍然需要在每个 GraphQL 操作中添加相同的响应 header 。请引用https://aws.amazon.com/about-aws/whats-new/2022/02/aws-appsync-support-custom-response-headers/和 https://docs.amazonaws.cn/appsync/latest/devguide/http-helpers-in-utils-http.html .
我创建了 VTL 模板并附加到 responseMappingTemplate 中的每个解析器。它对我有用。
关于aws-lambda - 如何在VTL模板中附加响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65540636/