访问 AWS AppSync 中的 json 数组
Accessing json array in AWS AppSync
我是 AWS AppSync 的新手,正在尝试使用 HTTP 端点从外部 API 获取数据。这是 API returns.
的示例
{
"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
我目前工作但只有return一个团队。我的问题是如何通过从 JSON 文件中的结果数组中获取所有项目来获取对 return 团队数组的 GraphQL 查询?
我不确定我是否真的理解您想要实现的目标,但它是这样的:
如果您想 return 一组团队而不是单个团队,您必须按如下方式修改架构中的查询:
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 正在 returning 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
}
然后在查询中使用别名 return 具有预期名称的字段,例如:
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"
},
....
我是 AWS AppSync 的新手,正在尝试使用 HTTP 端点从外部 API 获取数据。这是 API returns.
的示例{
"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
我目前工作但只有return一个团队。我的问题是如何通过从 JSON 文件中的结果数组中获取所有项目来获取对 return 团队数组的 GraphQL 查询?
我不确定我是否真的理解您想要实现的目标,但它是这样的:
如果您想 return 一组团队而不是单个团队,您必须按如下方式修改架构中的查询:
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 正在 returning 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
}
然后在查询中使用别名 return 具有预期名称的字段,例如:
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"
},
....