首页 > 解决方案 > 如何将 Hal+Json 与 AWS AppSync 结合使用

问题描述

我的后端使用媒体类型 hal+json(rest api) 返回 json。这里的问题是 AWS AppSync 的速度解析器返回:

词法错误,遇到:\"_\" (95),之后:\".\" at unset [第 8 行,第 28 列]"

因为媒体类型节点以下划线开头,例如:

{  
     "_embedded":{  
         "vehicle-assemblers":[...]
     }
}

我相信 AppSync 使用的引擎抱怨以下划线开头的变量。

“配置请求映射模板”(AWS 编辑解析程序)

 ## Raise a GraphQL field error in case of a datasource
 #if($ctx.error)
   $util.error($ctx.error.message, $ctx.error.type)
 #end
 ## If the response is not 200 then return an error.
 #if($ctx.result.statusCode == 200)
    #set($response = $util.parseJson($ctx.result.body))  
    $util.toJson($response._embedded.vehicle-assemblers) ##line 8  
 #else
    $utils.appendError($ctx.result.body, $ctx.result.statusCode)
 #end

有谁知道如何解决这个问题?

标签: amazon-web-servicesaws-appsynchal-json

解决方案


我能够解决

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

#if($ctx.result.statusCode == 200)
    #set($response = $util.parseJson($ctx.result.body))
    #set($vehicles = $util.toJson($response["_embedded"]["vehicle-assemblers"]))
    {
      "items": $vehicles  
    }    
#else
    $utils.appendError($ctx.result.body, $ctx.result.statusCode)
#end

推荐阅读