graphql - 如何在 GraphQL 中为键值定义枚举
问题描述
如何在 GraphQL 模式中定义枚举键?我期待的响应如下所示
businessByState: {
MO: ["VALUE1", "VALUE2"],
CA: ["VALUE1", "VALUE3", "VALUE4]
}
我知道我可以为状态值定义枚举,但仍然想知道如何定义枚举,以便键值只能是 2 个字母的状态缩写?
解决方案
在 GraphQL 中,必须显式定义 Object 类型的每个字段。例如:
type BusinessByState {
AL: [String!]!
AK: [String!]!
AZ: [String!]!
# and so on...
}
没有用于基于某些输入(如现有枚举)定义具有相同类型的多个字段的语法。
如果您的 typeDefs 只是一个字符串,您可以使用字符串模板来节省一些输入,假设您有某种状态缩写数组:
const states = ['AL', 'AK', 'AZ', /** and so on **/]
const typeDefs = `
enum STATES {
${states.join('\n')}
}
type BusinessByState {
${states.map(state => `${state}: [String!]!`).join('\n')}
}
`
推荐阅读
- javascript - 反应透视动画麻烦
- r - 为另一列中的每个唯一值分配一个名称
- python - 根据多个位置删除numpy数组中的项目
- python - 如何使用 threadpoolexecutor 和请求在 python 中发送带有多个请求的有效负载?
- python - 将字符串转换后的字节转换回字符串
- javascript - 从 [key, value] 对象中删除对象
- python - Polyglot - 如何从句子标签中获得信心
- android - 使用 MaterialButtonToggleGroup 更改所选 MaterialButton 的可绘制对象
- python - 我只想显示包含医院的部门
- python - 无法在文本框中输入文本,我可以单击它但无法通过 send_keys() 向其中添加文本