问题描述
我已将以下 GraphQL 架构上传到我的 FaunaDB:
type RSheet @collection(name: "sheets") {
columns: [RColumn!]! @relation
sections: [RSection]!
}
type RColumn @collection(name: "columns") {
label: String!
sheet: RSheet!
}
type RSection @embedded {
label: String
items: [[RItem!]]!
}
type RItem @embedded {
value: String
form: RItemForm
column: RColumn!
}
enum RItemForm {
STRING
}
这是一张包含列和多个部分的工作表,其中包含构成表格行的项目。
现在,我已经创建了一个工作表和两列。
“sheets” 数据库中的集合
{
"ref": Ref(Collection("sheets"),"304268724760740364"),"sections": [
{
"label": null,"items": []
}
]
}
“列”数据库中的集合
{
"ref": Ref(Collection("columns"),"304268724764934668"),"label": "Begin","sheet": Ref(Collection("sheets"),"304268724760740364")
},{
"ref": Ref(Collection("columns"),"304268724776469004"),"label": "End","304268724760740364")
}
到目前为止,所有这些都运行良好。我可以创建工作表和/或列、更新和删除它们。
但是,如果我想使用 FaunaDB 的 GraphQL 添加一个部分项目,我无法connect该项目的列。我收到一条错误消息,指出我必须提供该列的 ID!
:
当我这样做时,只会将 ID 写入数据库,并且不会创建连接 Ref(…)
:
“sheets” 数据库中的集合
{
"ref": Ref(Collection("sheets"),"sections": [
label: null
{
"items": [
[
{
"value": "Item 1","column": "304268724791149068" /// EXPECTED: Ref(Collection("columns"),"304268724791149068")
}
]
]
}
]
}
我错过了什么?
解决方法
您不能在嵌入类型中创建关系。来自docs on relations:
关系识别算法查看所有非嵌入类型的每个字段
我确实认为让关系通过 @embedded
类型工作会很好,但这并不是它们的真正用途,因此不受支持。
您需要重新设计架构,以便每个连接都处于非嵌入类型。此外,二维数组也不可能有连接。
示例
考虑以下架构
type RSheet @collection(name: "sheets") {
columns: [RColumn!]! @relation
sections: [RSection]! @relation
}
type RColumn @collection(name: "columns") {
label: String!
sheet: RSheet! @relation
items: [RItem!] @relation
}
type RSection @collection(name: "section") {
label: String
sheet: RSheet @relation
items: [RSectionItemList!]! @relation
}
type RSectionItemList @collection(name: "section_item_list") {
section: RSection! @relation
items: [RItem!]! @relation
}
type RItem @collection(name: "item") {
value: String
form: RItemForm
column: RColumn! @relation
list: RSectionItemList @relation
}
enum RItemForm {
STRING
}
注意我是如何将二维数组拆分为包装器列表对象的列表的。
可以像这样创建工作表和列:
并且添加的项目如下: