I've been stuggling to find the correct way to model a 'context dependent' field in 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. 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. The actionToComplete
field is only relevant when querying the Step within a Document context. 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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.