如何解决在Appsync graphql中过滤一对多精确数据
我对此建模:
type FreightDriver
@model
@key(
name: "byCityByState"
fields: ["state","city"]
queryField: "freightDriversByStateByCity"
) {
id: ID!
name: String!
state: String!
city: String!
trucks: [Truck] @connection(keyName: "byFreightDriver",fields: ["id"])
}
type Truck
@model
@key(name: "byFreightDriver",fields: ["freightDriverId","tons"]) {
id: ID!
freightDriverId: ID!
boxId: ID!
brand: String!
model: String!
tons: Float!
box: Box @connection(fields: ["boxId"])
}
type Box @model {
id: ID!
type: String!
width: Float!
height: Float!
depth: Float!
}
我正在这样查询数据:
query {
freightDriversByStateByCity(state: "Jalisco",city: { eq: "Guadalajara" }) {
items {
id
name
city
state
trucks(tons: { eq: 12 }) {
items {
brand
model
box {
type
}
}
}
}
}
}
我得到的回应是:
{
"data": {
"freightDriversByStateByCity": {
"items": [
{
"id": "aebb6696-573d-41ed-894e-22b69264cace","name": "Diey","city": "Guadalajara","state": "Jalisco","trucks": {
"items": [
{
"brand": "chevrolet","model": "12","box": {
"type": "Refrigerada"
}
}
]
}
},{
"id": "6e8e6772-61e7-47d6-b134-d615a3c65f62","name": "Roberto Mendez","trucks": {
"items": []
}
},{
"id": "d314808c-64e7-421d-b83b-008177ab6b25",{
"id": "19847d0c-185c-48f6-9e5c-435e3907133a","name": "Andrés","model": "1234","box": {
"type": "Refrigerada"
}
},{
"brand": "chevrolet","box": {
"type": "Grúa"
}
},{
"id": "2c4c9e3a-bfe5-4d4e-bee7-3eddb1b8ef1b","name": "Roberto Mendez ",{
"id": "cb4eb22c-aa54-416e-aecc-305a18bc9c83","trucks": {
"items": []
}
}
]
}
}
}
如果您检查,有一些与此有关:
"trucks": {
"items": []
}
如何避免它们?我的模型是错误的模型吗?谢谢!
解决方法
您可以使用映射模板修改响应数据,然后将其返回给客户端:
响应映射模板是使用Apache Velocity模板语言(VTL)编写的,并将解析器的结果转换回GraphQL。
在这种情况下,您可以检查trucks.items
是否为空,如果是,则在返回之前将items
中的trucks
数组删除。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。