简体   繁体   English

GraphQL:如何嵌套来制作模式?

[英]GraphQL: How nested to make schema?

This past year I converted an application to use Graphql.去年我将一个应用程序转换为使用 Graphql。 Its been great so far, during the conversion I essentially ported all my services that backed my REST endpoints to back grapqhl queries and mutations.到目前为止一切都很好,在转换过程中,我基本上移植了支持我的 REST 端点的所有服务以支持 grapqhl 查询和突变。 The app is working well but would like to continue to evolve my object graph.该应用程序运行良好,但希望继续改进我的对象图。

Lets consider I have the following relationships.让我们考虑一下我有以下关系。

User -> Team -> Boards -> Lists -> Cards -> Comments用户 -> 团队 -> 看板 -> 列表 -> 卡片 -> 评论

I currently have two different nested schema: User -> team:我目前有两个不同的嵌套模式:用户 -> 团队:

    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]
}

Then I have Boards -> Lists -> Cards -> Comments然后我有 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
}

Which works great.效果很好。 But I'm curious how nested I can truly make my schema.但我很好奇如何嵌套才能真正制作我的架构。 If I added the rest to make the graph complete:如果我添加其余部分以使图表完整:

type Team {
      id: ID!
      inviteToken: String!
      owner: String!
      name: String!
      archived: Boolean!
      members: [String]
      **boards: [Board]**
    }

This would achieve a much much deeper graph.这将实现更深层次的图形。 However I worried how much complicated mutations would be.但是我担心会有多少复杂的突变。 Specifically for the board schema downwards I need to publish subscription updates for all actions.特别是对于向下的板架构,我需要发布所有操作的订阅更新。 Which if I add a comment, publish the entire board update is incredibly inefficient.如果我添加评论,发布整个董事会更新是非常低效的。 While built a subscription logic for each create/update of every nested schema seems like a ton of code to achieve something simple.虽然为每个嵌套模式的每个创建/更新构建订阅逻辑似乎需要大量代码来实现一些简单的事情。

Any thoughts on what the right depth is in object graphs?关于对象图中正确深度的任何想法? With keeping in mind the every object beside a user needs to be broadcast to multiple users.请记住,用户旁边的每个对象都需要广播给多个用户。

Thanks谢谢

GraphQL's purpose is to avoid a couple of queries, so I'm sure that making the nested structure is the right way. GraphQL 的目的是避免几个查询,所以我确信制作嵌套结构是正确的方法。 With security in mind, add some GraphQL depth limit libraries.考虑到安全性,添加一些 GraphQL 深度限制库。

GraphQL style guides suggest you have all complex structures in separate Object Types ( as you have, Comment, Team, Board... ). GraphQL 风格指南建议您将所有复杂结构都放在单独的对象类型中(如您所拥有的,评论、团队、董事会...)。 Then making a complex query/mutation is up to you.然后进行复杂的查询/更改取决于您。

I'd like you to expand this sentence我希望你扩展这句话

Which if I add a comment, publish the entire board update is incredibly inefficient如果我添加评论,发布整个董事会更新效率非常低

I'm not sure about this as you have your id of the Card.我不确定这一点,因为您有卡的 ID。 So adding new comment will trigger mutation which will create new Comment record and update Card with the new comment.因此,添加新评论将触发突变,这将创建新的评论记录并使用新评论更新卡片。

So your structure of data on the backend will define the way you fetch it but not so much the way you mutate it.所以你在后端的数据结构将定义你获取它的方式,而不是你改变它的方式。

Take a look at the GitHub GraphQL API for example:GitHub GraphQL API为例:

  • each of the mutations is a small function for updating/creating piece of the complex tree even if they have nested structure of types on the backend.每个突变都是一个小函数,用于更新/创建复杂树的一部分,即使它们在后端具有嵌套的类型结构

In addition for general knowledge of what are approaches for designing the mutations, I'd suggest this article .除了有关突变设计方法的一般知识外,我还建议阅读这篇文章

You can use nesting in GraphQL like您可以在GraphQL中使用嵌套,例如

type NestedObject {
  title: String
  content: String
}

type MainObject {
  id: ID!
  myObject: [NestedObject]
}

In the above code, the type definition of NestObject gets injected into the myObject array.在上面的代码中, NestObject的类型定义被注入到myObject数组中。 To understand better you can see it as:为了更好地理解,您可以将其视为:

type MainObject {
  id: ID!
  myobject: [
    {
      title: String
      content: String
    }
  ]
}

I Hope this solves your problem!我希望这能解决你的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM