在 appsync graphql 中过滤一对多精确数据

Posted

技术标签:

【中文标题】在 appsync graphql 中过滤一对多精确数据【英文标题】:Filter one to many exact data in appsync graphql 【发布时间】:2020-12-11 09:37:34 【问题描述】:

我已经模拟了这个:

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",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": 
            "items": []
          
        ,
        
          "id": "d314808c-64e7-421d-b83b-008177ab6b25",
          "name": "Roberto Mendez",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": 
            "items": []
          
        ,
        
          "id": "19847d0c-185c-48f6-9e5c-435e3907133a",
          "name": "Andrés",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": 
            "items": [
              
                "brand": "chevrolet",
                "model": "1234",
                "box": 
                  "type": "Refrigerada"
                
              ,
              
                "brand": "chevrolet",
                "model": "1234",
                "box": 
                  "type": "Grúa"
                
              ,
              
                "brand": "chevrolet",
                "model": "12",
                "box": 
                  "type": "Refrigerada"
                
              
            ]
          
        ,
        
          "id": "2c4c9e3a-bfe5-4d4e-bee7-3eddb1b8ef1b",
          "name": "Roberto Mendez ",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": 
            "items": []
          
        ,
        
          "id": "cb4eb22c-aa54-416e-aecc-305a18bc9c83",
          "name": "Roberto Mendez",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": 
            "items": []
          
        
      ]
    
  

如果你检查,有一些是这样的:

"trucks": 
  "items": []

我怎样才能避免它们?我的模型是不是模型错误?谢谢!

【问题讨论】:

【参考方案1】:

您可以使用映射模板在响应数据返回给客户端之前对其进行修改:

响应映射模板使用 Apache Velocity 模板语言 (VTL) 编写,并将解析器的结果转换回 GraphQL。

在这种情况下,您可以检查 trucks.items 是否为空,如果是,请在返回之前从 trucks 中删除 items 数组。

更多详情:https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-programming-guide.html

【讨论】:

谢谢!上帝保佑你:)

以上是关于在 appsync graphql 中过滤一对多精确数据的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 AppSync GraphQL 转换为一对多 @connection 执行 @auth?

如何过滤graphQL一对多关系中的查询?

AWS AppSync - 从 AppSync 控制台运行时的 GraphQL 查询超时

如何使用 amplify 将 GraphQL 更改从 Appsync 同步到 Android?

Graphql apollo 客户端从 AppSync 返回空值

如何在 AppSync GraphQL 架构中使用自定义输入类型?