[英]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.