0

我一直在按照 AWS GraphQL CLI 指南为我的应用程序设置 API,但在连接时遇到了问题。

以下是我当前的 Graphql 架构,删除了一些属性

type Employee @model {
  id: ID!
  employment: [Employment!] @connection(name: "byEmployeeIDByCompanyID", fields: ["id"])
}

type Company @model {
  id: ID!
  employees: [Employment!] @connection(name: "byCompanyIDByDateHired", fields: ["id"])
}

type Employment @model
  @key(name: "byEmployeeIDByCompanyID", fields: ["employeeID", "companyID"], queryField: "employmentByEmployeeIDByCompanyID") {
  id: ID!
  companyID: ID!
  employeeID: ID!
  company: Company! @connection(fields: ["companyID"])
  employee: Employee! @connection(fields: ["employeeID"])
}

当我查询员工或公司时,[Employment] 总是返回一个空数组。我需要编辑这些字段的解析器吗?他们应该开箱即用,不是吗?

据我了解,使用带有“名称”和多个“字段”的@key 在表上创建一个二级索引,并用@connection 指定该键告诉连接使用该键而不是表主索引。例如,在“byEmployeeIDByCompanyID”键中,employeeID 是分区键,companyID 是排序键。“employmentByEmployeeIDByCompanyID”查询字段中带有employeeID 但没有companyID 的查询会返回给定员工的所有工作,这正是我想要的,那么为什么连接不起作用?

4

1 回答 1

0

我发现编辑解析器成功了,所以我现在要这样做。对于 Employee.employment,我添加了:

"index": "byEmployeeIDByCompanyID",

到请求映射模板,并将查询从:

{
  "expression": "#partitionKey = :partitionKey",
  "expressionNames": {
      "#partitionKey": "id"
  },
  "expressionValues": {
      ":partitionKey": {
          "S": "$context.source.id"
    }
  }
}

{
  "expression": "#partitionKey = :partitionKey",
  "expressionNames": {
      "#partitionKey": "employeeID"
  },
  "expressionValues": {
      ":partitionKey": {
          "S": "$context.source.id"
    }
  }
}
于 2020-11-10T15:28:16.433 回答