在AWS AppSync中访问json数组

问题描述 投票:0回答:1

我是AWS AppSync的新手,正在尝试使用HTTP端点从外部API提取数据。这是API返回的示例。

{
  "status": 1,
  "size": 3,
  "result": [
    {
      "number": "123",
      "program": "program name",
      "team_name": "team name",
      "robot_name": "robot name",
      "organisation": "team organization",
      "city": "team city",
      "region": "team state",
      "country": "team country",
      "grade": "team grade",
      "is_registered": 0
    },
    {
      "number": "456",
      "program": "program name",
      "team_name": "team name",
      "robot_name": "robot name",
      "organisation": "team organization",
      "city": "team city",
      "region": "team state",
      "country": "team country",
      "grade": "team grade",
      "is_registered": 0
    },
    {
      "number": "789",
      "program": "program name",
      "team_name": "team name",
      "robot_name": "robot name",
      "organisation": "team organization",
      "city": "team city",
      "region": "team state",
      "country": "team country",
      "grade": "team grade",
      "is_registered": 0
    }
  ]
}

这是我的GraphQL模式

type Query {
    getTeams(number: String!): Team
}

type Team {
    number: String
    program: String
    teamName: String
    robotName: String
    organization: String
    city: String
    region: String
    country: String
    grade: String
    isRegistered: Int
}

schema {
    query: Query
}

这是我的请求映射模板

{
    "version": "2018-05-29",
    "method": "GET",
    "resourcePath": "/v1/get_teams",
    "params":{
        "query": {
            "APIKEY": "API_KEY_GOES_HERE"
        },
        "headers": {
            "Content-Type": "application/json"
        }
    }
}

这是我的响应映射模板

#if($context.result.statusCode == 200)
## Success - decode the body and reconstruct the response with the schema in mind
#set($response = $util.parseJson($context.result.body))

#set($result = {
 "number": $response.result[0].number,
 "program": $response.result[0].program,
 "teamName": $response.result[0].team_name,
 "robotName": $response.result[0].robot_name,
 "organization": $response.result[0].organisation,
 "city": $response.result[0].city,
 "region": $response.result[0].region,
 "country": $response.result[0].country,
 "grade": $response.result[0].grade,
 "isRegistered": $response.result[0].is_registered
})
$util.toJson($result)
#else
## Error - send the proper error message
$utils.appendError($ctx.result.body, $ctx.result.statusCode)
#end

我目前正在工作,但只返回一个团队。我的问题是如何通过从JSON文件的结果数组中获取所有项来获取GraphQL查询以返回团队数组?

graphql aws-appsync
1个回答
0
投票

我不确定我是否真的了解您要实现的目标,但是可以解决:

如果要返回一组团队而不是单个团队,则必须按如下所示修改架构中的查询:

type Query {
    getTeams: [Team]
}

现在在响应映射上,您可以将响应直接映射到数组:

#if($ctx.result.statusCode == 200)
    ## If response is 200, return the body.
    $util.toJson($util.parseJson($ctx.result.body).result)
#else
    ## If response is not 200, append the response to error block.
    $utils.appendError($ctx.result.body, "$ctx.result.statusCode")
#end

此时,我注意到您已重命名架构中的字段,因此不再匹配;例如,您的json返回team_name,但您的graphQL模式期望为teamName

我要做的是修改架构以使其与JSON匹配,如下所示:

type Team {
    number: String
    program: String
    team_name: String
    robot_name: String
    organisation: String
    city: String
    region: String
    country: String
    grade: String
    is_registered: Int
}

然后在查询中使用别名返回具有期望名称的字段,例如:

query{
  getTeams{
    number: number
    teamName:team_name
    robotName: robot_name
    organization: organisation
  }
}

将产生我认为您期望的输出:

{
  "data": {
    "getTeams": [
      {
        "number": "123",
        "teamName": "team name",
        "robotName": "robot name",
        "organization": "team organization"
      },
      ....

© www.soinside.com 2019 - 2024. All rights reserved.