我正在使用 aws-amplify 来定义我的 appsync 架构。
我有以下 简化版 架构:
type Country @model {
id: ID!
name: String!
code: String!
tracks: [Track] @connection(name: "CountryTrack")
}
type Track @model
id: ID!
name: String!
country: Country! @connection(name: "CountryTrack")
lng: Float
lat: Float
length: Float
curves: Int
website: String
trackImage: String
information: String
}
Amplify 为模型生成 FilterInput。但是,它不包括连接类型。
我想根据国家过滤轨道。
dynamodb 表确实有
trackCountryId
在扫描操作中,我可以简单地根据 id 进行过滤。但是,这在 graphql 模式中不起作用。因为
trackCountryId
不包含在 FiterInput 中。有谁知道如何解决这个问题?
最佳答案
放大 CLI 创建 listTracks
查询+解析器,以及 tracks
Country
上的解析器类型,开箱即用。如果您想根据 countryId 过滤所有 Tracks,您必须在以下步骤中手动添加它,这些步骤本质上是上述生成的查询+解析器的混合:
-> 在您的架构中 : 在 type Query
中添加这个,然后单击“保存架构”:
listTracksByCountry(countryId: ID!, limit: Int, nextToken: String, sortDirection: ModelSortDirection): ModelTrackConnection
-> 附加解析器 到您刚刚添加的此查询字段,然后单击“保存解析程序”:
#set( $limit = $util.defaultIfNull($context.args.limit, 10) )
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "#connectionAttribute = :connectionAttribute",
"expressionNames": {
"#connectionAttribute": "trackCountryId"
},
"expressionValues": {
":connectionAttribute": {
"S": "$context.args.countryId"
}
}
},
"scanIndexForward": #if( $context.args.sortDirection )
#if( $context.args.sortDirection == "ASC" )
true
#else
false
#end
#else
true
#end,
"filter": #if( $context.args.filter )
$util.transform.toDynamoDBFilterExpression($ctx.args.filter)
#else
null
#end,
"limit": $limit,
"nextToken": #if( $context.args.nextToken )
"$context.args.nextToken"
#else
null
#end,
"index": "gsi-CountryTrack"
}
#if( !$result )
#set( $result = $ctx.result )
#end
$util.toJson($result)
-> 导航到 查询 部分,然后运行以下查询:
query {
listTracksByCountry(countryId: "countryId1") {
items {
id
name
length
}
}
}
您应该能够获得您指定的 countryId 的轨道列表。就我而言,上述操作的 GraphQL 输出是:
{
"data": {
"listTracksByCountry": {
"items": [
{
"id": "trackId1",
"name": "track name 1",
"length": 1.1
},
{
"id": "trackId2",
"name": "track name 2",
"length": 1.2
}
]
}
}
}
这似乎是一个非常常见的用例,因此请随时创建问题 here ,如果它不存在,然后我们可以使用 Amplify CLI (
amplify add api
),自动生成这些解析器。
关于amazon-web-services - 如何在 AppSync/Amplify 中过滤非标量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55000366/