【问题标题】:AWS App Sync dynamodb resolver usage with begin_with expression in Sort Key not workingAWS Appsync dynamodb 解析器在 Sort Key 中使用 begin_with 表达式不起作用
【发布时间】:2019-03-05 21:53:51
【问题描述】:

我正在尝试获取属于特定用户名的宠物列表。

表格设计(PetTable):

username: partition key
petId: sort key, 

petId 的值是通过连接字符串“petId:”和随机的 autoId 值生成的。所以,如果autoId是3838380022,那么petId排序键的值就是“petId:3838380022”

架构:

type Pet {
    username: String!
    petId: ID!
}

type PetsConnection {
    pets: [Pet]
}

type Query {
    getPets(username: String): PetsConnection
}

解析器:

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        ## Provide a query expression. **
        "expression": "username = :username and begins_with(petId, :petId)",
        "expressionValues" : {
            ":username" : {
                "S" : "${ctx.args.username}"
            },
            ":petId" : {
                "S" : "pet"
            }

        }
    }
}

查询:

query GetUserPets {
  getPets(username: "test") {
    pets {
      petId
    }
  }
}

查询响应:

{
  "data": {
    "getPets": {
      "pets": null
    }
  }
}

在 Dynamodb 中,我有 2 个整体,其中 petId(SortKey) 以文本 pet.

开头

期望查询应该返回 2 个条目,但它不返回任何内容。不知道错误在哪里。任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: amazon-dynamodb graphql aws-appsync


    【解决方案1】:

    我试图重现您的架构并尽可能多地对其进行测试。对我来说很好用

    type Pet {
        username: String!
        petID: ID!
    }
    
    type Query {
        getPets(username: String!): [Pet]
    }
    
    schema {
        query: Query
    }
    

    确保在为PetTable 创建资源时(用户名为PK,petID 为sort-key

    为查询附加解析器

    DynamoDB

    检测结果

    【讨论】:

      【解决方案2】:

      您的响应映​​射模板是什么? 您可能保留了返回结果列表的默认映射模板,而不是将 DynamoDB 中的查询结果映射到您的 PetConnection 类型。

      我复制了您的 API,并且能够使用相同的 GraphQL 查询获得结果。

      这是我的响应模板

      #set($petConnection = { 'pets': $ctx.result.items })
      $util.toJson($petConnection)
      

      结果:

      {
        "data": {
          "getPets": {
            "pets": [
              {
                "petId": "petId:1234"
              },
              {
                "petId": "petId:12345"
              }
            ]
          }
        }
      }
      

      注意:调试应用程序的一种好方法是从 API 设置页面启用日志。 请参阅下文如何启用日志。启用后,前往查询窗格,勾选日志复选框并执行 GraphQL 查询。您的应用程序日志应显示在控制台中。

      【讨论】:

        猜你喜欢
        • 2019-12-04
        • 2019-01-06
        • 2020-01-03
        • 2021-07-18
        • 1970-01-01
        • 2018-12-15
        • 2021-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多