简体   繁体   English

GraphQL 模式设计 - 上下文相关字段

[英]GraphQL schema design - context dependent fields

I've been stuggling to find the correct way to model a 'context dependent' field in GraphQL.我一直在努力寻找在 GraphQL 中对“上下文相关”字段进行建模的正确方法。 Here's an example of the data structure for a workflow system.这是工作流系统的数据结构示例。 Documents move through different steps in the workflow:文档在工作流程中通过不同的步骤:

workflow {
  steps {
    name
  },
  documents {
    type,
    step {
      name,
      actionToComplete
    }
  }
}

Most of it is straight forward.大部分都是直截了当的。 A Workflow has multiple Steps.一个工作流有多个步骤。 Within a Workflow there are multiple Documents.在一个工作流中有多个文档。 Each Document is in a certain Step.每个文档都在某个步骤中。 To move to the next Step, an action needs to be completed by the User.要进入下一步,用户需要完成一个操作。

The thing I'm struggling with is the actionToComplete field.我正在努力解决的问题是actionToComplete字段。 The value of the field is determined by the Context in which it is queried.该字段的值由查询它的上下文决定。 So depending on the logged in User, his Role in the system and the Document (its type, does it contain sensitive info, attachments, etc), the actionToComplete is determined.因此,根据登录用户、他在系统中的角色和文档(其类型、是否包含敏感信息、附件等),确定actionToComplete The actionToComplete field is only relevant when querying the Step within a Document context. actionToComplete字段仅在查询 Document 上下文中的 Step 时相关。 It's not relevant when just querying all the Workflow's steps, as it would not contain any meaningful data.仅查询工作流的所有步骤时,它并不相关,因为它不包含任何有意义的数据。

The issues I'm struggling with:我正在努力解决的问题:

  • The 'actionToComplete' field's value can only be determined when the Step is queried from a Document. 'actionToComplete' 字段的值只能在从文档中查询 Step 时确定。 When querying a Workflow's Steps we can not provide this field because we are missing the Document context.在查询工作流的步骤时,我们无法提供此字段,因为我们缺少文档上下文。
  • You could argue that the 'actionToComplete' field should be moved to the Document itself.您可能会争辩说 'actionToComplete' 字段应该移到 Document 本身。 This seems to create a disconnect with how people perceive this though.这似乎与人们如何看待这一点产生了脱节。 The natural question to ask seems to be “which action do I need to take to complete the Step the Document is in?”自然要问的问题似乎是“我需要采取什么行动来完成文档所在的步骤?” as opposed to “Which action do I need to complete on the Document?“.而不是“我需要在文档上完成哪些操作?”。 The data structure would also become less structured when more fields would be pushed up to the Document.当更多的字段被推送到文档时,数据结构也会变得不那么结构化。
  • Should we create a context specific version of the Step?我们应该创建 Step 的上下文特定版本吗? The Document's step field could then return a context specific DocumentStep type which contains the 'actionToComplete' field.然后,文档的步骤字段可以返回包含“actionToComplete”字段的特定于上下文的 DocumentStep 类型。 The Workflow's steps field would return a list of regular Step types.工作流的步骤字段将返回常规步骤类型的列表。 This doesn't feel right because the Step and DocumentStep types are exactly the same, just viewed from a different context这感觉不对,因为 Step 和 DocumentStep 类型完全相同,只是从不同的上下文中查看

Does anybody have any advice on how to model something like this?有人对如何建模这样的东西有任何建议吗?

I personally don't see anything wrong with moving the field to the Document type -- you may be overthinking things.我个人认为将字段移动到Document类型没有任何问题——您可能想多了。 The field is still related to the document, even if conceptually it also relates to the step.该字段仍然与文档相关,即使在概念上它也与步骤相关。 It'd be different if you had multiple steps per document but that's not the case here.如果每个文档有多个步骤,情况会有所不同,但这里的情况并非如此。 I also don't really see the data being flatter as a major concern.我也并不真正将数据扁平化视为主要问题。

That said, this is a good use case for explicitly creating two separate types.也就是说,这是显式创建两个独立类型的一个很好的用例。 It's perfectly fine to have different types that represent the same domain model, particularly if you need to expose different fields depending on the parent field.拥有代表相同域模型的不同类型非常好,特别是如果您需要根据父字段公开不同的字段。 We have a tendency to avoid duplication in our schemas, but in this case it makes perfect sense.我们倾向于避免模式中的重复,但在这种情况下,这是完全合理的。

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

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