如何在无服务器框架中为POST端点添加请求模型

如何为POST端点添加请求模型,以便在导出API for iOS时显示在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的APGateway SDK来使用authentication凭证,而不是手动执行https。

它看起来像无服务器项目的lib / Endpoint.js不包括requestModels条目,但项目是积极维护,所以也许你可以提出一个问题,在他们的GitHub增加支持。 与此同时,我认为分享AWS CLI方法可能会有所帮助。

您创build请求模型的方式与您创build响应模型的方式相同,但是创build请求模型并不存在与将请求模型与方法相关联的aws apigateway put-method-response等效的简单命令。 这似乎是AWS CLI的一个缺失function​​。

不过,我使用aws apigateway update-method 。 您需要先为该请求创build模型,然后该命令将其添加到方法中。

 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的斜杠停止解释为path的一部分。

顺便说一句,我试过,并没有得到JSON文件的参数 – --patch-operations工作。 如果有人可以阐明为什么这个文件导致下面的错误,我很想听到它。

 $ 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: {