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

查看:135
本文介绍了如何在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天全站免登陆