我的应用程序在serverless框架中,我使用vtl模板作为lambda resolver。我的应用程序栈是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上面的代码给出了邮递员的结果,但是我无法在响应部分看到我的自定义标题。我想我错过了如何在响应vtl中包含标题。
发布于 2022-07-08 09:48:21
虽然为时已晚,但我仍然需要在每个GraphQL操作中添加相同的响应头。请参考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中的每个解析器上。对我来说很管用。
https://stackoverflow.com/questions/65540636
复制相似问题