【问题标题】:Appsync resolver response template error: Unable to convert to objectAppsync 解析器响应模板错误:无法转换为对象
【发布时间】:2020-12-20 19:41:16
【问题描述】:

我有一个appsync 速度响应映射模板,它返回令人讨厌的错误

"Unable to convert \n          \n           ,           \n       ,           \n  \n\n{\"channels\":[{\"createdAt\":\"2020-09-01T14:25:32.796Z\",\"featured\":false,\"thumb\":\"http://www.google.com\",\"__typename\":\"Channel\",\"description\":\"The test channel\",\"id\":\"73bf0ef9-2d91-4b78-90f8-7548444fdb8f\",\"title\":\"The test title\",\"notifications\":false,\"tags\":[\"awesome\",\"action\"],\"updatedAt\":\"2020-09-01T14:25:32.796Z\"}],\"creators\":[],\"shows\":[{\"createdAt\":\"2020-09-01T14:40:46.996Z\",\"thumb\":\"http://www.google.com\",\"author\":\"Steven Colbert\",\"__typename\":\"Show\",\"description\":\"The tonight show with Steven Colbert\",\"id\":\"5a95234e-6d30-4396-bc05-b607eb4276f6\",\"source\":\"http://google.com\",\"title\":\"The tonight show\",\"channelId\":\"dce40626-76ae-45ba-97a9-a2b29d2234c9\",\"live\":false,\"tags\":[\"Tonight show\"],\"updatedAt\":\"2020-09-01T14:40:46.996Z\"},{\"createdAt\":\"2020-09-01T14:46:40.406Z\",\"thumb\":\"http://www.google.com\",\"author\":\"Seth Miers\",\"__typename\":\"Show\",\"description\":\"The Late show with Seth Miers\",\"id\":\"09d3d573-8737-411c-ae82-ee3600565e04\",\"source\":\"http://google.com\",\"title\":\"The Late show\",\"channelId\":\"dce40626-76ae-45ba-97a9-a2b29d2234c9\",\"live\":false,\"tags\":[\"Late show\"],\"updatedAt\":\"2020-09-01T14:46:40.406Z\"}]}\n to Object."

我不明白为什么会这样?该对象似乎是正确的,并且 graphQl 模式需要一个具有三个属性的对象,每个属性都应包含一个对象数组。

我检查了elasticsearch请求的日志,请求模板发出的响应是正确的。这是我从 elasticsearch 得到的对象:

{
    "logType": "ResponseMapping",
    "path": [
        "search"
    ],
    "fieldName": "search",
    "requestId": "c8946f14-8bb7-46a8-a23f-4b2defc0a9cd",
    "context": {
        "arguments": {
            "filter": {
                "title": {
                    "contains": "The"
                }
            }
        },
        "result": {
            "took": 36,
            "timed_out": false,
            "_shards": {
                "total": 11,
                "successful": 11,
                "skipped": 1,
                "failed": 0
            },
            "hits": {
                "total": {
                    "value": 3,
                    "relation": "eq"
                },
                "hits": [
                    {
                        "_index": "channel",
                        "_type": "_doc",
                        "_id": "73bf0ef9-2d91-4b78-90f8-7548444fdb8f",
                        "_score": 0,
                        "_source": {
                            "createdAt": "2020-09-01T14:25:32.796Z",
                            "featured": false,
                            "thumb": "http://www.google.com",
                            "__typename": "Channel",
                            "description": "The test channel",
                            "id": "73bf0ef9-2d91-4b78-90f8-7548444fdb8f",
                            "title": "The test title",
                            "notifications": false,
                            "tags": [
                                "awesome",
                                "action"
                            ],
                            "updatedAt": "2020-09-01T14:25:32.796Z"
                        },
                        "sort": [
                            "channel",
                            0
                        ]
                    },
                    {
                        "_index": "show",
                        "_type": "_doc",
                        "_id": "5a95234e-6d30-4396-bc05-b607eb4276f6",
                        "_score": 0,
                        "_source": {
                            "createdAt": "2020-09-01T14:40:46.996Z",
                            "thumb": "http://www.google.com",
                            "author": "Steven Colbert",
                            "__typename": "Show",
                            "description": "The tonight show with Steven Colbert",
                            "id": "5a95234e-6d30-4396-bc05-b607eb4276f6",
                            "source": "http://google.com",
                            "title": "The tonight show",
                            "channelId": "dce40626-76ae-45ba-97a9-a2b29d2234c9",
                            "live": false,
                            "tags": [
                                "Tonight show"
                            ],
                            "updatedAt": "2020-09-01T14:40:46.996Z"
                        },
                        "sort": [
                            "show",
                            0
                        ]
                    },
                    {
                        "_index": "show",
                        "_type": "_doc",
                        "_id": "09d3d573-8737-411c-ae82-ee3600565e04",
                        "_score": 0,
                        "_source": {
                            "createdAt": "2020-09-01T14:46:40.406Z",
                            "thumb": "http://www.google.com",
                            "author": "Seth Miers",
                            "__typename": "Show",
                            "description": "The Late show with Seth Miers",
                            "id": "09d3d573-8737-411c-ae82-ee3600565e04",
                            "source": "http://google.com",
                            "title": "The Late show",
                            "channelId": "dce40626-76ae-45ba-97a9-a2b29d2234c9",
                            "live": false,
                            "tags": [
                                "Late show"
                            ],
                            "updatedAt": "2020-09-01T14:46:40.406Z"
                        },
                        "sort": [
                            "show",
                            0
                        ]
                    }
                ]
            }
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Query"
}

这是我的响应映射模板。我不喜欢 VTL 模板,也不喜欢 VTL 模板,但我不明白为什么它抱怨它无法将看似有效的对象转换为对象。

        #set( $channels = [] )
        #set( $creators = [] )
        #set( $shows = [] )

        #foreach($entry in $context.result.hits.hits)
          ## $velocityCount starts at 1 and increments with the #foreach loop **
          #if( $velocityCount > 1 ) , #end
          #set ($item = $entry.get("_source"))
          #set ($itemType = $entry.get("_index"))
          #if ($itemType == "channel")
            $util.qr($channels.add($item))
          #end
          #if ($itemType == "creator")
            $util.qr($creators.add($item))
          #end
          #if ($itemType == "show")
            $util.qr($shows.add($item))
          #end
        #end

        #set ($response = { "channels": $channels,"creators": $creators,"shows": $shows } )

        $util.toJson($response)

我也试过这个,结果和错误相同:

#set( $channels = [] )
#set( $creators = [] )
#set( $shows = [] )
#set( $hits = $context.result.hits.hits )
#set ($response = {} )

#foreach($entry in $hits)
  ## $velocityCount starts at 1 and increments with the #foreach loop **
  #if( $velocityCount > 1 ) , #end
  #set ($item = $entry.get("_source") )
  #set ($itemType = $entry.get("_index") )
  #if ($itemType == "channel")
    $util.qr( $channels.add($item) )
  #end
  #if ($itemType == "creator")
    $util.qr( $creators.add($item) )
  #end
  #if ($itemType == "show")
    $util.qr( $shows.add($item) )
  #end
#end

$util.qr($response.put("channels", $channels))
$util.qr($response.put("creators", $creators))
$util.qr($response.put("shows", $shows))

$util.toJson($response)

我的 graphQl Schema 的相关部分:

type Query @aws_api_key
@aws_iam {
    search(filter: SearchFilterInput, size: Int, from: Int): SearchFilterConnectionModel
}

input SearchFilterInput {
    title: ModelStringFilterInput
    tag: ModelStringFilterInput
    userName: String
}

type SearchFilterConnectionModel @aws_api_key
@aws_iam {
    channels: [Channel]
    shows: [ListShowConnectionModel]
    creators: [Creator]
}

type ListShowConnectionModel @aws_api_key
@aws_iam {
    id: ID!
    createdAt: String
    updatedAt: String
    description: String
    source: String
    author: String
    thumb: String
    title: String
    tags: [String!]
    live: Boolean
    channelId: String
}

type Channel @aws_api_key
@aws_iam {
    id: ID!
    createdAt: String
    updatedAt: String
    tags: [String!]
    title: String
    description: String
    thumb: String
    featured: Boolean
    notifications: Boolean
}

type Creator @aws_api_key
@aws_iam {
    id: ID!
    createdAt: String
    updatedAt: String
    cognitoSub: String
    userName: String
    thumb: String
    bio: String
    channelsOwned: [ChannelsListModel!]
    showsOwned: [ShowsListModel!]
}

【问题讨论】:

    标签: amazon-web-services aws-appsync vtl


    【解决方案1】:

    最后想通了。

    这是foreach循环中的那一行

    #if( $velocityCount > 1 ) , #end
    

    这是将 , 添加到返回的项目列表中,使响应成为无效的 json

    【讨论】:

      【解决方案2】:

      我能够重现您遇到的错误,我认为问题出在您的 foreach 语句中,您在该语句中访问了两次 hits

      这是它按预期工作的方式并且非常好:

      #set( $channels = [] )
      #set( $creators = [] )
      #set( $shows = [] )
      
      #set( $testCtx = [{"_source" : {"createdAt":"2020-09-01T14:25:32.796Z","featured":false,"thumb":"http://www.google.com","__typename":"Channel","description":"The test channel","id":"73bf0ef9-2d91-4b78-90f8-7548444fdb8f","title":"The test title","notifications":false,"tags":["awesome","action"],"updatedAt":"2020-09-01T14:25:32.796Z"}, "_index": "channel"}] )
      
      #foreach($entry in $testCtx)
        ## $velocityCount starts at 1 and increments with the #foreach loop **
        #if( $velocityCount > 1 ) , #end
        #set ($item = $entry.get("_source"))
        #set ($itemType = $entry.get("_index"))
        #if ($itemType == "channel")
          $util.qr($channels.add($item))
        #end
        #if ($itemType == "creator")
          $util.qr($creators.add($item))
        #end
        #if ($itemType == "show")
          $util.qr($shows.add($item))
        #end
      #end
      
      #set ($response = { "channels": $channels,"creators": $creators,"shows": $shows } )
      
      $util.toJson($response)
      

      如果我将foreach 语句更改为#foreach($entry in $testCtx[0]),我会遇到与您一样愚蠢的无法转换错误。除此之外,我已经对您的代码进行了多次测试,并且运行良好。

      我希望这会帮助你,或者至少给你一些方向来指出问题。

      【讨论】:

      • 关于您对 for 循环的评论并多次访问 hits.hits,我已将我从 elasticsearch 获得的回复添加到我的问题中。
      猜你喜欢
      • 2021-11-04
      • 2021-05-30
      • 2021-12-13
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多