2016-06-10 24 views
2

如何爲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。

回答

1

看起來無服務器項目的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: { 
相關問題