如何在graphql方案中迭代json嵌套数组 [英] How to iterate json nested array in graphql scheme

查看:44
本文介绍了如何在graphql方案中迭代json嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 NodeJS Apollo 服务器有以下 JSON 输出:

I have the following JSON output from my NodeJS Apollo server:

attributes:
   [ { id: 8,
       name: 'Size',
       position: 0,
       visible: true,
       variation: true,
       options: [Array] } ],

现在我想为此创建一个 graphql 方案.我可以访问所有项目,如 id、位置、可见......但我无法访问的选项.

Now I want to create a graphql scheme for this. I can access all the items like id, position, visible.. but the options I can't access.

我该如何为此编写 graphql 方案?目前的方案是:

How can I write the graphql scheme for this? Current scheme is:

 const ProductAttributes = `
      type ProductAttributes {
        id: Int!
        name: String!
        position: String!
        visible: Boolean!
        Variation: Boolean!
        // todo Options: [array] ?
      }
    `;

我发现 options 数组看起来像这样,但没有分配给它的键:

I found out the options array looks like this but no key is assigned to it:

[ { 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 请求中执行此操作?

Do I have to assign a key to this with GraphQL ? or do I have to do this in my NodeJS ajax request?

例如:选项:{名称:'L32 W35',名称:'l32 W36',等等...

e.g.: options: { name: 'L32 W35', name: 'l32 W36', etc...

推荐答案

如果 [Array] 是 object 的数组,您需要这样做.

If [Array] is array of object you need to do that.

`
type ProductAttributes { 
        id: Int!
        name: String!
        position: String!
        visible: Boolean!
        Variation: Boolean!
        options: [String!] 
}
`

如果 [Array] 是 array 的数组,那么在每个最后一个内部数组中,对象或字符串都应采用上述方式.并且您可以处理这些步骤直到最后一个数组.现在在你的字符串数组中,你可以直接使用 String 类型.

If [Array] is an array of the array then in every last inner array should object or string take as above. And you can process these steps till the last array. Now in your case array of string so you can use direct String type.

这篇关于如何在graphql方案中迭代json嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆