【发布时间】:2019-07-26 10:37:14
【问题描述】:
我们有 db 集合,它有点复杂。我们的许多键都是 JSON 对象,其中字段不是固定的,而是根据用户在 UI 上给出的输入而变化。对于这种复杂的类型,我们应该如何编写 mongoose 和 GraphQL Schema?
{
"_id" : ObjectId("5ababb359b3f180012762684"),
"item_type" : "Sample",
"title" : "This is sample title",
"sub_title" : "Sample sub title",
"byline" : "5c6ed39d6ed6def938b71562",
"lede" : "Sample description",
"promoted" : "",
"slug" : [
"myurl"
],
"categories" : [
"Technology"
],
"components" : [
{
"type" : "Slide",
"props" : {
"description" : {
"type" : "",
"props" : {
"value" : "Sample value"
}
},
"subHeader" : {
"type" : "",
"props" : {
"value" : ""
}
},
"ButtonWorld" : {
"type" : "a-button",
"props" : {
"buttonType" : "product",
"urlType" : "Internal Link",
"isServices" : false,
"title" : "Hello World",
"authors" : [
{
"__dataID__" : "Qm9va0F1dGhvcjo1YWJhYjI0YjllNDIxNDAwMTAxMGNkZmY=",
"_id" : null,
"First_Name" : "John",
"Last_Name" : "Doe",
"Display_Name" : "John Doe",
"Slug" : "john-doe",
"Role" : 1
}
],
"isbns" : [
"9781497603424"
],
"image" : "978-cover.jpg",
"price" : "8.99",
"bisacs" : [],
"customCategories" : [],
},
"salePrice" : {
"type" : "",
"props" : {
"value" : ""
}
}
}
},
"tags" : [
{
"id" : "5abab58042e2c90011875801",
"name" : "Tag Test 1"
},
{
"id" : "5abab5831242260011c248f9",
"name" : "Tag Test 2"
},
{
"id" : "592450e0b1be5055278eb5c6",
"name" : "horror",
},
{
"id" : "59244a96b1be5055278e9b0e",
"name" : "Special Report",
"_id" : "59244a96b1be5055278e9b0e"
}
],
"created_at" : ISODate("2018-03-27T21:44:21.412Z"),
"created_by" : ObjectId("591345bda429e90011c1797e")
}
我相信 Mongoose 有 Mixed 类型,但我如何在 Apollo GraphQL Server 和 Mongoose Schema 中表示如此复杂的类型。另外,目前我的解析器只是models.product.find()。因此,如果我有如此复杂的类型,需要了解需要对我的解析器进行哪些更新。
如果我能获得 GraphQL Apollo 模式、猫鼬模式和数据解析器的完整解决方案,那就太好了。
【问题讨论】:
-
既然已经有很多类似的问题,我认为我们应该关闭这个问题。 @jvm,您可以删除此问题,如果您愿意,可以对您认为有帮助的其他问题的任何答案进行投票
标签: graphql apollo-server