Ad

How To Iterate Json Nested Array In Graphql Scheme

- 1 answer

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

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

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.

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] ?
      }
    `;

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' ] } ]

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

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

Ad

Answer

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!] 
}
`

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.

Ad
source: stackoverflow.com
Ad