Search code examples
graphqlexpress-graphql

GraphQl Array Schema


I have a response from an external API genre_ids: [0: 16, 1: 21, 2: 46]; At this point, I have no clue how to define a schema for something like that.


Solution

  • not sure what 0: 1: 2: etc. are, but i assume, that it is index in the array. If you would like to implement arrays of integers or ids you should use GraphQL modifiers. It is called GraphQL List and you can do in your schema something like this in graphql-js.

    genreIds: {
      type: new GraphQLList(GraphQLID),
      resolve: () => { 
       // your external api call
       return [ 16, 21, 46] 
      }
    }
    

    If you would like to know more detail. I have recently published article on implementing arrays in GraphQL schema. Here is the full code on how you can implement fetching array of users. Btw. not sure what you would like to do with these ids, but it is better schema architecture to replace it with GraphQL object type and fetche the genres list based on these ids. I hope that it helps.

    David