node.js - 如何在 graphql 模式中迭代 json 嵌套数组

标签 node.js reactjs graphql apollo

我的 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] 是数组的数组,那么在每个最后一个内部数组中,对象或字符串应采用如上所示。您可以处理这些步骤直到最后一个数组。 现在在您的情况下是字符串数组,因此您可以使用直接 String 类型。

关于node.js - 如何在 graphql 模式中迭代 json 嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55018992/

相关文章:

reactjs - React useRef 更改不会导致依赖于 ref.current 的组件重新渲染

database - 图 QL + PostgreSQL : Need an ORM or just query builder?

node.js - 在 ElasticSearch 上搜索和排序

node.js - 在 NodeJS 和 MongoDB 中未定义的 toArray

javascript - React.js 处理事件浏览器丢失

javascript - 过滤器 vs map Reactjs 和 jsx

java - 有没有办法将 GraphQL 查询字段分组为子/嵌套查询组?

graphql - Android apollo 订阅不起作用?

node.js - 将 https 代理配置为仅允许 TLS1.2 用于传出请求

node.js - Socket.io access-control-allow=来自远程站点的来源错误