【问题标题】:How to filter data ignoring the data that does not match如何过滤数据忽略不匹配的数据
【发布时间】:2020-12-02 20:46:49
【问题描述】:

我有这个问题:

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 匹配的数据作为响应,即 Jalisco。

{
  "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 查询的外观如何?

【问题讨论】:

    标签: aws-amplify aws-appsync


    【解决方案1】:

    我经常看到这个问题,这让我有点不安全,但 GraphQL 不应该以这种方式工作。你应该得到你想要的,而不是“SQL 查询自己的胜利”。

    随便,

    您可以通过过滤掉所有 trucks.items.length 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 设计?我对卡车一无所知,但也许

    • 改为“位置有卡车有司机”? 或
    • “位置有司机有卡车”?

    甚至两者都有!由于 GraphQL 为您提供了您想要的东西,Driver 可以包含 Truck,而 Truck 可以包含 Driver。

    Location {
    id: ID!
    truck: [Truck]
    driver: [Driver]
    }
    
    Truck {
     id: ID!
     driver: Driver!
    }
    
    Driver {
     id: ID!
     Truck: Truck!
    }
    

    使用深度 2 放大自动生成,这样您的列表就不会永远循环下去,而且您可以不要求不需要的东西。这里有很多选择。

    https://docs.amplify.aws/cli/graphql-transformer/dataaccess


    如果你想让它成为一个 Lambda (@function),dynamo 语法非常简单(并且几乎相同)。

    要么扫描整个表https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#scan-property 或者你创建一个你查询的索引然后过滤https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property


    最后但并非最不重要

    【讨论】:

    • 谢谢!是的,我会重新考虑一个小模式模型来获取数据,将@key 添加到这个
    猜你喜欢
    • 2015-04-20
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多