AWS Amplify - Graphql + 数据存储:变量 'input' 已强制 NonNull 类型字符串的 Null 值

AWS Amplify - Graphql + Datastore: Variable 'input' has coerced Null value for NonNull type String

运行 使用 aws amplify 生成的 API 出现问题。

基本上,每当我尝试创建一个实体并且它没有被持久保存在 DynamoDB 中时,我都会不断收到以下警告。

Variable 'input' has coerced Null value for NonNull type 'String!


以下是我用来创建后端的 Graphql 模式的相关部分。

enum EntityStatus {
     ACTIVE
     INACTIVE
     ARCHIVED
    }

type Address {
  streetAddress1: String!
  streetAddress2: String
  city: String!
  state: String!
  zipCode: String!
  country: String!
  location: Location!
}

type Location {
  lat: Float
  lng: Float
}

type Tenant
  @model
  @auth(
    rules: [
      { allow: groups, groups: ["Admin", "Coordinator", "Employees"], operations }
      { allow: groups, groups: ["Auditor"], operations: [read] }
    ]
  ) {
  id: ID!
  name: String!
  address: Address!
  phone: AWSPhone!
  email: AWSEmail!
  status: EntityStatus!
  locale: String!
}

创建 Tenant 个实体之一的代码是一个简单的调用

    try {
      return await DataStore.save(new Tenant({ ...values }));
    } catch (error) {
      console.error(error);
    }

Datastore 发送的负载如下:

{
  "name": "Tenant 1",
  "phone": "1234567890",
  "email": "tenant@tenant.com",
  "status": "ACTIVE",
  "address": {
    "city": "Anytown",
    "state": "TAB",
    "zipCode": "12345",
    "country": "US",
    "location": { "lat": 123.12, "lng": 123.12 }
  },
  "locale": "en-US",
  "id": "f8be53bd-b1cb-4cbd-9b64-01fdf930da8a"
}

这是完整的警告消息

[WARN] 40:26.787 DataStore 
Object { localModel: {…}, message: "Variable 'input' has coerced Null value for NonNull type 'String!'", operation: "Create", errorType: undefined, errorInfo: undefined, remoteModel: null }
errorInfo: undefined
errorType: undefined
localModel: Object { id: "f8be53bd-b1cb-4cbd-9b64-01fdf930da8a", name: "Tenant 1", phone: "1234567890", … }
_deleted: undefined
_lastChangedAt: undefined
_version: undefined
address: Object { city: "Anytown", state: "TAB", zipCode: "12345", … }
createdAt: undefined
email: "tenant@tenant.com"
id: "f8be53bd-b1cb-4cbd-9b64-01fdf930da8a"
locale: "en-US"
name: "Tenant 1"
phone: "1234567890"
status: "ACTIVE"
updatedAt: undefined
<prototype>: Object { … }
message: "Variable 'input' has coerced Null value for NonNull type 'String!'"
operation: "Create"
remoteModel: null
<prototype>: Object { … }
react_devtools_backend.js:3973:25

想通了。我的负载缺少 2 个字段。

希望错误消息能更有帮助。