[英]GraphQL: How nested to make schema?
去年我將一個應用程序轉換為使用 Graphql。 到目前為止一切都很好,在轉換過程中,我基本上移植了支持我的 REST 端點的所有服務以支持 grapqhl 查詢和突變。 該應用程序運行良好,但希望繼續改進我的對象圖。
讓我們考慮一下我有以下關系。
用戶 -> 團隊 -> 看板 -> 列表 -> 卡片 -> 評論
我目前有兩個不同的嵌套模式:用戶 -> 團隊:
type User {
id: ID!
email: String!
role: String!
name: String!
resetPasswordToken: String
team: Team!
lastActiveAt: Date
}
type Team {
id: ID!
inviteToken: String!
owner: String!
name: String!
archived: Boolean!
members: [String]
}
然后我有 Boards -> Lists -> Cards -> Comments
type Board {
id: ID!
name: String!
teamId: String!
lists: [List]
createdAt: Date
updatedAt: Date
}
type List {
id: ID!
name: String!
order: Int!
description: String
backgroundColor: String
cardColor: String
archived: Boolean
boardId: String!
ownerId: String!
teamId: String!
cards: [Card]
}
type Card {
id: ID!
text: String!
order: Int
groupCards: [Card]
type: String
backgroundColor: String
votes: [String]
boardId: String
listId: String
ownerId: String
teamId: String!
comments: [Comment]
createdAt: Date
updatedAt: Date
}
type Comment {
id: ID!
text: String!
archived: Boolean
boardId: String!
ownerId: String
teamId: String!
cardId: String!
createdAt: Date
updatedAt: Date
}
效果很好。 但我很好奇如何嵌套才能真正制作我的架構。 如果我添加其余部分以使圖表完整:
type Team {
id: ID!
inviteToken: String!
owner: String!
name: String!
archived: Boolean!
members: [String]
**boards: [Board]**
}
這將實現更深層次的圖形。 但是我擔心會有多少復雜的突變。 特別是對於向下的板架構,我需要發布所有操作的訂閱更新。 如果我添加評論,發布整個董事會更新是非常低效的。 雖然為每個嵌套模式的每個創建/更新構建訂閱邏輯似乎需要大量代碼來實現一些簡單的事情。
關於對象圖中正確深度的任何想法? 請記住,用戶旁邊的每個對象都需要廣播給多個用戶。
謝謝
GraphQL 的目的是避免幾個查詢,所以我確信制作嵌套結構是正確的方法。 考慮到安全性,添加一些 GraphQL 深度限制庫。
GraphQL 風格指南建議您將所有復雜結構都放在單獨的對象類型中(如您所擁有的,評論、團隊、董事會...)。 然后進行復雜的查詢/更改取決於您。
我希望你擴展這句話
如果我添加評論,發布整個董事會更新效率非常低
我不確定這一點,因為您有卡的 ID。 因此,添加新評論將觸發突變,這將創建新的評論記錄並使用新評論更新卡片。
所以你在后端的數據結構將定義你獲取它的方式,而不是你改變它的方式。
除了有關突變設計方法的一般知識外,我還建議閱讀這篇文章。
您可以在GraphQL
中使用嵌套,例如
type NestedObject {
title: String
content: String
}
type MainObject {
id: ID!
myObject: [NestedObject]
}
在上面的代碼中, NestObject
的類型定義被注入到myObject
數組中。 為了更好地理解,您可以將其視為:
type MainObject {
id: ID!
myobject: [
{
title: String
content: String
}
]
}
我希望這能解決你的問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.