【发布时间】:2021-02-25 23:52:18
【问题描述】:
我目前正在为我当地的比萨店开发比萨订购/跟踪系统。正如您在下面看到的,我定义了 GraphQL 类型和 Mongoose 模型。在我的比萨饼中,我设置了一系列浇头来引用浇头模型。我的问题是,我的突变定义和使用所需参数对该突变的调用是什么样的?
我希望 Mongoose 中的 Pizza 文档包含相关的 ToppingId,以便我可以引用该浇头的其他字段,例如名称和价格。希望这很清楚。也许数组不是解决这个问题的最佳方法??
如果在披萨模型中有更有效的浇头方法,请分享。
GraphQL 类型:
type Topping {
id: ID!
name: String!
price: Float!
createdAt: String!
}
type Pizza {
id: ID!
location: String!
price: Float!
toppings: [Topping]!
createdAt: String!
toppingCount: Int!
}
猫鼬模型:
const pizzaSchema = new Schema({
location: String,
price: Number,
toppings: {
type: Schema.Types.ObjectId,
ref: "toppings"
},
createdAt: String
});
const toppingSchema = new Schema({
name: String,
price: Number,
createdAt: String
});
因此,理想情况下,在数据库中查看披萨时,它应该如下所示:
_id: ObjectId("5fab2e6cc4b5e628685228f6")
> toppings:Array
[
ObjectId("5fab2e6cc4b5e628685228f7")
ObjectId("5fab2e6cc4b5e628685228f8")
]
location:"Barcelona"
price:123
createdAt:"12"
__v:0
提前致谢。
编辑:我的突变类型和突变调用/Args + 错误消息
createPizza(pizzaInput: PizzaInput): Pizza!
input PizzaInput {
location: String!
price: Float!
toppings: [ID]!
createdAt: String!
}
mutation {
createPizza(pizzaInput: {
location:"Barcelona"
price:12
toppings: [
"5faaca3129c74ff103d3ccaf"
"5faaed887ac3c53010b8075b"
]
createdAt:"AAAA"
}){
id
price
location
toppings {
id
}
createdAt
toppingCount
}
}
错误信息:
"ID 不能代表值:
【问题讨论】:
标签: node.js mongodb mongoose graphql