我的目标是编写一个应用程序,用户可以在其中添加项目列表(假设20-30)到每个条目…每天有一个条目,每次条目的数量不同。
我有两个像这样的GraphQL类型:
type Entry @model @auth(rules: [{allow: public}]) {
id: ID!
date: String!
entries: [Item] @hasMany
}
type Item @model @auth(rules: [{allow: public}]) {
id: ID!
name: String!
received: Boolean!
quantity: String!
hazardRating: Int!
entry: Entry @belongsTo
comments: [Comment] @hasMany
}
type Comment @auth(rules: [{allow: public}]) @model {
id: ID!
item: Item @belongsTo
content: String!
}
我想写一个突变,将Item添加到现有的Entry:
我已经试过了:
mutation createItem{
createItem( input:{name: "Sodium Hydroxide", received: true, quantity: "1L", hazardRating: 3, entry: {id = "7a59cfca-db53-4f15-8ae6-c37e025b2a44", date = "21 October 2022" }) {
id
name
received
quantity
hazardRating
entry {
id
date
}
}
}
但是我得到了错误信息"entry">
我如何编写一个突变,将Item添加到现有的Entry中?
是否有可能这样做,或者是否只能同时添加每个条目的所有条目?
Item中缺少引用。
为了将Item识别为Entry的一部分,您必须添加entryID: ID!@index(name: "byEntry", sortKeyFields: ["name"])到Item中,也有一个indexName参数到entry中:(indexName: "byEntry", fields: ["id"])
与GraphQL v1 - v2不同的是,当您从头开始模式时,它无法自动完成此操作,因此您必须确保每种类型相互引用。
整个方案是这样的:
type Entry @model @auth(rules: [{allow: public}]) {
id: ID!
date: String!
items: [Item] @hasMany(indexName: "byEntry", fields: ["id"])
}
type Item @model @auth(rules: [{allow: public}]) {
id: ID!
entryID: ID! @index(name: "byEntry", sortKeyFields: ["name"])
name: String!
amount: String!
hazardRating: Int!
comments: [Comment] @hasMany(indexName: "byItem", fields: ["id"])
}
type Comment @model @auth(rules: [{allow: public}]) {
id: ID!
itemID: ID! @index(name: "byItem" sortKeyFields: ["content"])
content: String!
}