我正在使用 amplify 编写 graphql 模式。我有一个类别表,我想在其中将每个类别与其他类别相关联。每个类别可以有多个父级或子级。 这就是我现在拥有的。
type Category @model {
id: ID! @primaryKey
name: String!
parents: [Category] @hasMany
subCategories:[Category] @hasMany
categoryParentsId: [String] @index(name: "byParent")
categorySubCategoriesId: [String] @index(name: "byChild")
}
type Query {
getAllCategories: [Category!]!
}
input CreateCategoryInput {
name: String!
categoryParentsId: [String]
categorySubCategoriesId: [String]
}
我在前端使用反应应用程序。每当我尝试输入时,Amplify 都会显示此错误。
“一个或多个参数值无效:索引键categoryParentsId 的类型不匹配预期:S 实际:L 索引名称:gsi-Category.parents(服务:DynamoDb,状态代码:400”
我知道它正在生成一个GSI,其中categoryParentsId和categorySubCategoriesId不能是字符串、数字或布尔值之外的其他值。所以它与我所说的字符串数组有点冲突。
我应该添加另一个存储该字符串数组的字段吗?有更好的方法吗?
您不需要同时引用父类型和子类型以及 GraphQL 类型中相应的 id:
type Category @model {
id: ID! @primaryKey
name: String!
parents: [Category] @hasMany
subCategories:[Category] @hasMany
}
应该足够了。
另请注明您正在尝试的突变的定义。您显示了
input type
,但没有显示突变。