GraphQL:利用它来处理复杂的视图/UI逻辑
我需要将复杂开关逻辑从UI层移动到GraphQL层。此复合体开关逻辑是基于一堆后端配置执行计算,以确定是否应可见一组UI组件。要在集合中显示或隐藏特定的UI组件,如果> - else
分支逻辑,它至少需要至少3个层次的深处
。我目前正在考虑将以下类型添加到架构中以解决此问题:
type MyComplexView {
componentAVisible: Boolean!
componentBVisible: Boolean!
# ... (gazzillion of other components here)
componentXVisible: Boolean!
}
...然后我希望从查询中返回的数据看起来像:
{
"data": {
"myComplexView": {
"componentAVisible": true,
"componentBVisible": false,
// ... (gazzillion of other components here)
"componentXVisible": true,
}
}
}
但是感觉不正确,感觉就像我正在滥用GraphQl层进行此类工作一样。
所以问题是,这也是使用GraphQl的有效用例吗?是否有另一种方法或更好的方法?这个想法是为所有客户端(例如Web和Mobile)共享将消耗API的复杂开关逻辑,而无需重新编写/重复客户端上的逻辑。
I've got a requirement to move complex switches logic from UI layer to GraphQL layer. This complex switches logic is to perform a computation based on a bunch of backend configurations to determine whether a set of UI components should be visible or not. To show or hide a particular UI component in the set, it requires at least 3 levels deep of nested if
-else
branches logic. I'm currently thinking of adding the following type to the schema to solve this problem:
type MyComplexView {
componentAVisible: Boolean!
componentBVisible: Boolean!
# ... (gazzillion of other components here)
componentXVisible: Boolean!
}
...and then I'd expect the data returned from the query would look something like:
{
"data": {
"myComplexView": {
"componentAVisible": true,
"componentBVisible": false,
// ... (gazzillion of other components here)
"componentXVisible": true,
}
}
}
But it just doesn't feel right, and it feels like I'm abusing the GraphQL layer for doing this kind of job.
So question is, is this also a valid use case for making use of GraphQL? Is there an alternative or better way of doing it? The idea is to share the complex switches logic for all the clients (e.g. web and mobile) that are going to consume the API without re-writing/duplicating the logic again on the client-side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论