【问题标题】:AWS AppSync Error: Template transformation yielded an empty responseAWS AppSync 错误:模板转换产生空响应
【发布时间】:2021-12-13 04:04:18
【问题描述】:

我在 AWS 云之外部署了一个 .Net Core GraphQL API。我正在尝试将 AWS AppSync 与我的 GraphQL API 集成。

架构:

  type TestData {
           StduentId: String
           StduentName: String
       }

  type Query {
      TestFunction(
             departmentId: String,
             departmentCity: String,
           ): [TestData]
    }

  schema {
           query: Query
         }

我正在 AWS AppSync 控制台中运行以下查询。

query
{
   TestFunction(
                 departmentId:"124",
                 departmentCity: "Boston"
               ) 
   {
       StduentId
       StudentName
   }

}

请求映射模板:

    {
        "version": "2018-05-29",
        "method": "GET",
        "resourcePath": "/",
        "params":{
        "query":$util.toJson($ctx.args),
         "headers": {
                       "Authorization": "$ctx.request.headers.Authorization"
                    }
            }
       }

响应映射模板。

#if($ctx.error)
   $util.error($ctx.error.message, $ctx.error.type)
#end

#if($ctx.result.statusCode == 200)
   $util.toJson($context.result),
#else
   $utils.appendError($ctx.result.body, "$ctx.result.statusCode")
#end

AppSync:数据源资源是:

https://myserver.com/graphql/

我期待上述查询执行的嵌套 JSON 输出。

查询响应: "errorType": "映射模板", "message": "模板转换产生空响应。"

CloudWatch 错误:
"message": "映射模板引发了自定义错误。",

【问题讨论】:

    标签: graphql aws-appsync webapi


    【解决方案1】:

    您是否正在尝试为 REST api 创建一个 http 解析器?我不确定为什么你的路径中有 /graphql?

    对于 appsync 中的 HTTP REST 解析器,您的数据源应以“.com”结尾。类似https://myserver.com

    然后在您的请求模板中资源路径完成指定的资源:

    { “版本”:“2018-05-29”, “方法”:“获取”, "resourcePath": "/v1/api/some-resource", “参数”:{ "查询":$util.toJson($ctx.args), “标题”:{ “授权”:“$ctx.request.headers.Authorization” } } }

    响应映射模板如下所示:

    #if($ctx.error)
       $util.error($ctx.error`enter code here`.message, $ctx.error.type)
    #end
    
    #if($ctx.result.statusCode == 200)
       #set($body = $util.parseJson($ctx.result.body))
        $util.toJson($body) #make it $body.data depending on the return on your rest api
    #else
       $utils.appendError($ctx.result.body, "$ctx.result.statusCode")
    #end
    

    【讨论】:

    • 我的实际网址是:myserver.com:12345。我已经更新了响应映射,但它仍然无法正常工作。但有些东西看起来更好。错误 ""message": "词法错误,遇到:\"`\" (96),在 *unset*[第 2 行,第 26 列] 处:\"\" 之后""
    • 我期待嵌套 Json 中的 TestData。 (函数返回类型为 List(TestData)
    猜你喜欢
    • 2020-12-20
    • 2021-11-04
    • 2022-01-23
    • 2019-07-19
    • 2020-04-22
    • 2018-06-15
    • 2017-02-17
    • 2019-02-23
    相关资源
    最近更新 更多