1

我使用 aws amplify 并有一个这样的 graphql 模式,我使用 amplify push 进行推送:

type User @model  {
  id: ID!
  lastname: String!
  customerID: ID!
  status: Status @connection
  statusID: ID!
}

type Status @model {
  id: ID!
  name: String
}

我可以在 appsync 控制台中插入一个新用户而不会出错:

mutation CreateUser($input: CreateUserInput!) {
  createUser(input: $input) {
    id
    customerID
    lastname
    status {
      id
      name
    }
  }
}

{"input": {
  "id": 1,
"customerID": 1,
  "statusID": 1
"lastname": "Doe"
}
}

但是,一旦我通过在 graphql 模式中添加 @key(fields: ["customerID"]) 创建具有主键 customerID 的相同 dynamodb 表用户,我在 appsync 控制台中收到以下错误:

 {
      "path": [
        "createUser",
        "status"
      ],
      "data": null,
      "errorType": "DynamoDB:AmazonDynamoDBException",
      "errorInfo": null,
      "locations": [
        {
          "line": 6,
          "column": 5,
          "sourceName": null
        }
      ],
      "message": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 4SUT0SN1R6BKS8KG2V1AGUCHGRVV4KQNSO5AEMVJF66Q9ASUAAJG)"
    }

尽管在数据库中正确创建了用户。除了 pk customerID 之外,我没有看到创建的代码中的 2 个表之间有任何区别。

有任何想法吗?

4

1 回答 1

2

您的架构建模“有一个”关系,您可以参考此文档以获取示例架构 https://docs.amplify.aws/cli/graphql-transformer/directives#has-one

不带参数的 @connection 指令将使用主键。Amplify CLI 还将为您生成以下输入类型。如果我正确理解您的意图,则您的架构中不需要 User 类型中的 statusID 字段。fields如果您想为连接使用特定字段(例如“statusID”),您可以在@connection 定义中指定参数,文档中也有一个示例。

执行创建突变时,应先创建 Status 对象,获取其 Id,然后创建 User 对象并将 userStatusId 指定为 Status Id。

input CreateUserInput {
  id: ID
  lastname: String!
  customerID: ID!
  statusID: ID!
  userStatusId: ID
}
于 2020-09-10T00:38:00.827 回答