[英]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:我正在努力解决的问题:
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.