javascript - Graphql (Gatsby.js) 中的条件模式类型
问题描述
我正在使用在 gatsby 构建时将数据注入到 graphql 的外部 API,我们的新系统有一个名为“value”的属性,它取决于一个名为“type”的相对属性,“value”属性的模式类型会有所不同。
类型 > 值
- “文本”-> 字符串
- “段落”-> 字符串
- 'HTML' -> 字符串
- “图像”-> 文件 @fileByCustomDirective
- '收音机'->字符串
- “选择”-> 字符串
- '复选框' - > [字符串]
上面显示了所有可能的类型字符串以及“值”属性的模式类型应该是什么。
我不确定这是否可能,但在 createSchemaCustomization 函数中,我将如何定义这样的类型,以及如何在前端查询它?
这就是我的架构的样子,不包括动态逻辑:
type customFields {
_id: String
value: String // this should be a array or File depending on the type value!
type: String
label: String
id: String
},
如果找不到解决方案,这是我正在考虑应用的解决方法:
type customFields {
_id: String
value: String
image: File @fileByCustomDirective // possible hack
values: [String] // possible hack
type: String
label: String
id: String
},
我唯一能想到的我知道现在可以工作的事情是通过添加另一个名为“image”的属性来强制 API 在类型为“image”时向我发送一个重复值,当类型为“values”时相同'复选框',虽然这感觉真的很hacky,如果可能的话,我希望一切都通过同一个属性。
解决方案
推荐阅读
- javascript - 如何用 Jest 测试 document.eventListener
- python - 限制脚本中的 Ram 使用
- android - 当我在导航抽屉外单击时,图标不会改变
- c# - 导出 Excel 数据时重定向到操作不起作用
- sql - jOOQ 中的隐式连接和 N:N 表
- c++ - 为类中声明的变量定义一个值
- laravel - VUEJS - 未知的自定义元素:
- 您是否正确注册了组件? - ios - Swift:仅从表中的某些行/部分中分离
- c# - 带有 EF Core 迁移和 IHttpContextAccessor 的 ASP.NET Core (3.1)
- javascript - 如何从 json 对象中获取总计数