amazon-web-services - 如何在 AppSync/Amplify 中过滤非标量类型

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

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

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

最佳答案

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

-> 在您的架构中 : 在 type Query 中添加这个,然后单击“保存架构”:

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

-> 附加解析器 到您刚刚添加的此查询字段,然后单击“保存解析程序”:
  • 选择 跟踪表作为您的数据源名称
  • 请求映射模板 :
  • #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"
    }
    
  • ResponseMapping模板 :
  • #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 ),自动生成这些解析器。

    关于amazon-web-services - 如何在 AppSync/Amplify 中过滤非标量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55000366/

    相关文章:

    database - SimpleDB 选择修改项

    amazon-web-services - 根据示例配置时,节点环境中的 AWS AppSync 会出现有关 asyncLocalStorage 的错误

    aws-lambda - AppSync 与 AWS4 和混合身份验证类型(用户池和 IAM)

    java - 如何使用 AWS DynamoDB QueryRequest 类中的 addQueryFilterEntry(String, Condition)?

    amazon-dynamodb - 错误 InvalidParameterType : Expected params. 项目 ['pid'] 成为 DynamoDB 中的结构

    amazon-web-services - 将 CloudFormation 参数传递给 AppSync Resolver

    c# - 我在执行 AWS Lambda 函数时遇到问题

    amazon-web-services - 暂停 Elastic Beanstalk 应用程序环境?

    javascript - Angular/Node.js : Create subdomain on aws server from code end

    amazon-web-services - 如何通过 API 创建启用 PITR 的表?