【问题标题】:How to filter a non-scalar type in AppSync / Amplify如何在 AppSync / Amplify 中过滤非标量类型
【发布时间】:2019-03-05 10:13:06
【问题描述】:

我正在使用 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 中。

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb aws-appsync aws-amplify amplifyjs


    【解决方案1】:

    Amplify CLI 开箱即用地创建listTracks 查询+解析器和tracks 解析器Country 类型。如果您想根据 countryId 过滤 所有 曲目,则必须在以下步骤中手动添加,这些步骤本质上是上述生成的查询 + 解析器的混合:

    -> 在您的架构中:将其添加到type Query,然后点击“保存架构”:

    listTracksByCountry(countryId: ID!, limit: Int, nextToken: String, sortDirection: ModelSortDirection): ModelTrackConnection
    

    -> 附加解析器到您刚刚添加的这个查询字段,然后点击“保存解析器”:

    • 选择 TrackTable 作为您的数据源名称

    • RequestMapping 模板

    #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) 自动生成这些解析器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-09
      • 2019-04-14
      • 2020-08-20
      • 2019-04-14
      • 2021-09-08
      • 2018-10-27
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多