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

问题描述:

如何为POST端点添加请求模型,以便在出口iOS API时显示在API网关中?我可以在AWS中手动添加请求模型,但由于这是通过服务器部署的,所以我需要在那里。我看到有一个用于定义端点的responseModels,但我看不到requestModels?如何在无服务器框架中为POST端点添加请求模型

我的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来使用认证凭证,而不是手动执行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解释为路径的一部分。

顺便说一句,我试过,并没有得到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: {