首页 > 解决方案 > Graphql (Gatsby.js) 中的条件模式类型

问题描述

我正在使用在 gatsby 构建时将数据注入到 graphql 的外部 API,我们的新系统有一个名为“value”的属性,它取决于一个名为“type”的相对属性,“value”属性的模式类型会有所不同。

类型 > 值

  1. “文本”-> 字符串
  2. “段落”-> 字符串
  3. 'HTML' -> 字符串
  4. “图像”-> 文件 @fileByCustomDirective
  5. '收音机'->字符串
  6. “选择”-> 字符串
  7. '复选框' - > [字符串]

上面显示了所有可能的类型字符串以及“值”属性的模式类型应该是什么。

我不确定这是否可能,但在 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,如果可能的话,我希望一切都通过同一个属性。

标签: javascriptgraphqlschemagatsby

解决方案


推荐阅读