如何为 POST 端点添加请求模型,以便在导出适用于 iOS 的 API 时它出现在 API 网关中?我可以在 AWS 中手动添加请求模型,但由于这是通过服务器部署的,所以我需要它。我看到有一个用于定义端点的responseModels,但我看不到requestModels?
我的 s-functions.json 有这个
"endpoints": [
{
"path": "blog/graphql",
"method": "POST",
"type": "AWS",
"authorizationType": "AWS_IAM",
"authorizerFunction": false,
"apiKeyRequired": false,
"requestParameters": {},
"requestTemplates": {
"application/json": "{\"query\" : $input.json(\"$\")}"
},
"responses": {
"400": {
"statusCode": "400"
},
"default": {
"statusCode": "200",
"responseParameters": {},
"responseModels": {},
"responseTemplates": {},
"application/json": ""
}
}
}
]
在 AWS APIGateway 中,我需要手动添加请求模型
{
"title": "Example Schema",
"type": "object",
"properties": {
"query": {
"type": "string"
}
},
"required": ["query"]
}
当我导出适用于 iOS 的 API 时,我确实获得了正确的方法来发送 graphQL 查询并且它可以工作。
但是,由于我想通过无服务器部署来部署它,所以我无法继续手动添加它。
我需要 API 端点调用通过 iOS 的 APIGateway SDK 才能使用 cognito 凭据,而不是手动执行 https。
最佳答案
看起来无服务器项目的 lib/Endpoint.js 不包含 requestModels 条目,但该项目正在积极维护,因此也许您可以在 GitHub 上提出问题以供他们添加支持。我认为同时分享一个 AWS CLI 方法可能会很有用。
您创建请求模型的方式与创建响应模型相同,但创建它们后,没有一个与 aws apigateway put-method-response
等效的简单命令将请求模型与方法关联起来。这似乎是 AWS CLI 缺失的功能。
但是,我使用aws apigateway update-method
使其正常工作。您需要先为请求创建模型,然后此命令将其添加到方法中。
aws apigateway update-method \
--region $region \
--rest-api-id "$rest_api_id" \
--resource-id "$resource_id" \
--http-method $method \
--patch-operations "op=add,path=/requestModels/application~1json,value=${request_model_name}"
请注意,奇怪的 application~1json
构造是为了阻止 application/json
中的斜杠被解释为路径的一部分。
顺便说一句,我尝试让 --patch-operations
的 JSON 文件参数正常工作,但失败了。如果有人能解释为什么这个文件会导致下面的错误,我很想听听。
$ cat patch.json
{
"patchOperations":[{
"op" : "add",
"path" : "/requestModels/application~1json",
"value" : "TestRequest"
}]
}
$ aws apigateway update-method \
--rest-api-id abc123 \
--resource-id def456 \
--http-method POST \
--patch-operations "file://patch.json"
Error parsing parameter '--patch-operations': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
JSON received: {
关于node.js - 如何在无服务器框架中为 POST 端点添加请求模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37748423/