node.js - 如何在graphql方案中迭代json嵌套数组
问题描述
我的 NodeJS Apollo 服务器有以下 JSON 输出:
attributes:
[ { id: 8,
name: 'Size',
position: 0,
visible: true,
variation: true,
options: [Array] } ],
现在我想为此创建一个 graphql 方案。我可以访问所有项目,如 id、位置、可见......但我无法访问的选项。
我该如何为此编写graphql方案?目前的方案是:
const ProductAttributes = `
type ProductAttributes {
id: Int!
name: String!
position: String!
visible: Boolean!
Variation: Boolean!
// todo Options: [array] ?
}
`;
我发现选项数组看起来像这样,但没有为其分配键:
[ { id: 8,
name: 'Size',
position: 0,
visible: true,
variation: true,
options:
[ 'L32 W35',
'L32 W36',
'L32 W37',
'L32 W38',
'L28 W22',
'L28 W23',
'L28 W24',
'L28 W25',
'L32 W34' ] } ]
我是否必须使用 GraphQL 为其分配一个键?还是我必须在我的 NodeJS ajax 请求中这样做?
例如:选项:{名称:'L32 W35',名称:'l32 W36'等...
解决方案
如果 [Array] 是对象数组,则需要这样做。
`
type ProductAttributes {
id: Int!
name: String!
position: String!
visible: Boolean!
Variation: Boolean!
options: [String!]
}
`
如果 [Array] 是数组的一个数组,那么在每个最后一个内部数组中,对象或字符串都应如上所述。您可以处理这些步骤直到最后一个数组。现在在您的情况下是字符串数组,因此您可以使用直接字符串类型。
推荐阅读
- c# - 使用导航属性将 DTO 映射到 EF POCO 的最佳实践
- javascript - 向 MongoDB 领域添加新用户
- javascript - 许多人在反应中重新渲染 TradingView 图表
- flutter - Flutter Calculator App UI 在大多数设备中溢出
- python - Pandas:如何将 Series 的每个元素与 Dataframe 中列的每个元素相乘
- reactjs - React js - “const example = () => {}”和“function example () {}”之间的区别
- excel - 我希望能够在不解锁的情况下从锁定的纸张上剪切
- vue.js - Vue.js 文件错误“该文件将不会显示在编辑器中,因为它是二进制的、非常大的或使用了不受支持的文本编码。”
- pine-script - 在另一种策略中使用来自一个指标的数据
- typescript - 当在打字稿中用作接口时,void 类型似乎覆盖了返回类型