首页 > 解决方案 > 未找到字段的值 - AppSync AWS

问题描述

我目前正在使用 AppSync 来查询 GSI。我已经能够在 Pipeline Resolver 函数中成功使用此代码块,但是当我尝试在传统解析器中使用它时,我不知道为什么它不起作用。

我目前收到映射模板错误:

{
  "data": {
    "units": null
  },
  "errors": [
    {
      "path": [
        "units"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Value for field '$[version]' not found."
    }
  ]
}

我尝试在 AWS 文档中搜索,但向 GraphQL 类型添加“版本”不起作用。

我也试过这个(即使我没有使用 S3) AppSync S3Object 检索 和文档: https ://docs.aws.amazon.com/appsync/latest/devguide/troubleshooting-and-common-mistakes.html#mapping -模板错误

这是请求映射模板:

#set($arg=$context.args)

    {
    "operation": "Query",
    "index" : "userPK-userSK-index",
    "query": {
        "expression": "userPK = :pk and begins_with(userSK, :sk)",
        "expressionValues": {
            ":pk": {"S": "tenant:${arg.tenantId}" },
            ":sk": {"S": "school-year:${arg.schoolYear}:grades:${arg.gradeId}:subject:${arg.subjectId}:unit:"}
        }
    }
}

这是响应映射模板:

$util.toJson($ctx.result.items)

这是执行的 GraphQL 的片段:

query GetUnits{
  units(tenantId: "5fc30406-346c-42e2-8083-fda33ab6000a"
schoolYear: "2019-2020"
    gradeId: "c737e341-a0cb-4a16-95de-f3a092049e74"
subjectId: "d0306e25-422d-4628-8fcc-c354b67c932a") {
  id
  indicator {
    id,
    description
  }
  competency {
    id,
    description,
    name
  }
  description,
  name
}


}

这是 GraphQL 模式的片段:


type Unit {
  id: ID!
  competency: Competency
  indicator: Indicator
  name: String!
  description: String
  version: Int
}

type Competency {
  id: ID
  # grade: Grade
  # subject: Subject
  # schoolYear: String
  name: String
  description: String
}

type Indicator { 
  id: ID!
  description: String
}
type Query {
  units(
    tenantId: String!
    schoolYear: String!
    gradeId: String!
    subjectId: String!
  ): [Unit]

以下是 DynamoDB 表中的数据示例: 在此处输入图像描述

这是控制台中成功查询的屏幕截图: 在此处输入图像描述

注意:我创建了一个 GSI,将 userPK 和 userSK 分别映射为分区键和排序键。我正在查询该二级索引。我已经能够使用控制台成功查询这个。

标签: amazon-web-servicesamazon-dynamodbgraphqlaws-appsync

解决方案


该错误表明您忘记了version参数。这是查询模板(docs):

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        "expression" : "some expression",
        "expressionNames" : {
            "#foo" : "foo"
        },
        "expressionValues" : {
            ":bar" : ... typed value
        }
    }
    "index" : "fooIndex",
    "nextToken" : "a pagination token",
    "limit" : 10,
    "scanIndexForward" : true,
    "consistentRead" : false,
    "select" : "ALL_ATTRIBUTES",
    "filter" : {
        ...
    }
}

并且version是必需的:

版本

模板定义版本。当前支持 2017-02-28 和 2018-05-29。该值是必需的。


推荐阅读