如何过滤数据忽略不匹配的数据
How to filter data ignoring the data that does not match
我有这个查询:
query ListFreightDriverTrucks($state: String! $tons: Float!) {
listFreightDrivers(filter: {
state: {
contains: $state
}
}) {
items {
name
city
state
trucks (filter: {
tons: {
eq: $tons
}
}) {
items {
id
brand
model
fuelType
fuelEfficiency
utilityPercentage
tons
axes
frontPhoto
truckBox {
type
width
height
depth
}
}
}
}
}
}
作为响应,我得到了与哈利斯科 $state
相匹配的数据。
{
"data": {
"listFreightDrivers": {
"items": [
{
"name": "Jaen Carlos",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Diey",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Roberto mendez",
"city": "Guadalajara",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Engineering",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Roberto mendez",
"city": "Guadalajara",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Andrés",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": [
{
"id": "2b0cb78e-49c4-4229-8a71-60b350a5fc47",
"brand": "chevrolet",
"model": "xx",
"fuelType": "magna",
"fuelEfficiency": 12,
"utilityPercentage": 10,
"tons": 15,
"axes": 12,
"frontPhoto": "freight-driver/e9adf7fb-09c2-477e-9152-56fe4a71a96b/trucks/dlb0275xqna51.png",
"truckBox": {
"type": "Plataforma",
"width": 4,
"height": 4,
"depth": 4
}
}
]
}
}
]
}
}
}
如果你检查回复,有一些是这样的:
"trucks": {
"items": []
}
但我对那些不感兴趣,因为与 $tons
不匹配,只是最后一个匹配。我怎样才能删除它们?
如果我需要创建一个 lambda,DynamoDB 查询会是什么样子?
我经常看到这个问题,这让我有点不安全,但 GraphQL 不应该那样工作。你应该得到你所要求的,而不是“SQL 查询自己的胜利”。
随便,
您可以在解析器(req.vtl 文件)中通过过滤掉所有 trucks.items.length < 1 或其他内容来解决此问题。请看这个 link
Appsync & GraphQL: how to filter a list by nested value
请注意,这是一个非常慢的 DynamoDB 扫描操作(所有列表操作都是)。
AWS DynamoDB 具有相同的设计理念,您大多数时候都知道您正在寻找的唯一键,并且只过滤少量项目。添加大量索引或组合键。
如果您想更新数据模型,推荐阅读:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html
也许重新考虑您的 GraphQL 设计?我对卡车一无所知,但也许
- “Location has Truck has Driver”而不是?
或者
- “位置 Driver 有卡车”?
甚至两者兼而有之!因为 GraphQL 给了你你想要的东西,所以 Driver 可以包含一辆卡车和一辆卡车 Driver.
Location {
id: ID!
truck: [Truck]
driver: [Driver]
}
Truck {
id: ID!
driver: Driver!
}
Driver {
id: ID!
Truck: Truck!
}
Amplify auto generates with depth 2 这样你的列表就不会永远循环,你就可以不问你不需要的东西。这里有很多选择。
https://docs.amplify.aws/cli/graphql-transformer/dataaccess
如果你想让它成为 Lambda (@function),dynamo 语法非常简单(而且几乎相同)。
要么你扫描全tablehttps://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#scan-property
或者你创建一个你查询的索引然后过滤 https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property
最后但并非最不重要的
我有这个查询:
query ListFreightDriverTrucks($state: String! $tons: Float!) {
listFreightDrivers(filter: {
state: {
contains: $state
}
}) {
items {
name
city
state
trucks (filter: {
tons: {
eq: $tons
}
}) {
items {
id
brand
model
fuelType
fuelEfficiency
utilityPercentage
tons
axes
frontPhoto
truckBox {
type
width
height
depth
}
}
}
}
}
}
作为响应,我得到了与哈利斯科 $state
相匹配的数据。
{
"data": {
"listFreightDrivers": {
"items": [
{
"name": "Jaen Carlos",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Diey",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Roberto mendez",
"city": "Guadalajara",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Engineering",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Roberto mendez",
"city": "Guadalajara",
"state": "Jalisco",
"trucks": {
"items": []
}
},
{
"name": "Andrés",
"city": "Zapopan",
"state": "Jalisco",
"trucks": {
"items": [
{
"id": "2b0cb78e-49c4-4229-8a71-60b350a5fc47",
"brand": "chevrolet",
"model": "xx",
"fuelType": "magna",
"fuelEfficiency": 12,
"utilityPercentage": 10,
"tons": 15,
"axes": 12,
"frontPhoto": "freight-driver/e9adf7fb-09c2-477e-9152-56fe4a71a96b/trucks/dlb0275xqna51.png",
"truckBox": {
"type": "Plataforma",
"width": 4,
"height": 4,
"depth": 4
}
}
]
}
}
]
}
}
}
如果你检查回复,有一些是这样的:
"trucks": {
"items": []
}
但我对那些不感兴趣,因为与 $tons
不匹配,只是最后一个匹配。我怎样才能删除它们?
如果我需要创建一个 lambda,DynamoDB 查询会是什么样子?
我经常看到这个问题,这让我有点不安全,但 GraphQL 不应该那样工作。你应该得到你所要求的,而不是“SQL 查询自己的胜利”。
随便,
您可以在解析器(req.vtl 文件)中通过过滤掉所有 trucks.items.length < 1 或其他内容来解决此问题。请看这个 link Appsync & GraphQL: how to filter a list by nested value
请注意,这是一个非常慢的 DynamoDB 扫描操作(所有列表操作都是)。
AWS DynamoDB 具有相同的设计理念,您大多数时候都知道您正在寻找的唯一键,并且只过滤少量项目。添加大量索引或组合键。
如果您想更新数据模型,推荐阅读: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html
也许重新考虑您的 GraphQL 设计?我对卡车一无所知,但也许
- “Location has Truck has Driver”而不是? 或者
- “位置 Driver 有卡车”?
甚至两者兼而有之!因为 GraphQL 给了你你想要的东西,所以 Driver 可以包含一辆卡车和一辆卡车 Driver.
Location {
id: ID!
truck: [Truck]
driver: [Driver]
}
Truck {
id: ID!
driver: Driver!
}
Driver {
id: ID!
Truck: Truck!
}
Amplify auto generates with depth 2 这样你的列表就不会永远循环,你就可以不问你不需要的东西。这里有很多选择。
https://docs.amplify.aws/cli/graphql-transformer/dataaccess
如果你想让它成为 Lambda (@function),dynamo 语法非常简单(而且几乎相同)。
要么你扫描全tablehttps://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#scan-property 或者你创建一个你查询的索引然后过滤 https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property
最后但并非最不重要的