When working with collections of things in GraphQL, we'll always reach out for the
GraphQLList Type. In this video, we'll learn how to use GraphQLList from the
graphql package in combination with a
GraphQLObject Type to create a field that returns a collection in our Schema.
On our query type, we have exposed a single field for video, which will just retrieve a single video. Like in our previous schema, we may want to retrieve all of the videos available to us. Let's go ahead and add another field called "videos" that is built off of a GraphQL list.
To start off, let's actually go into our data folder inside of index.js. Let's add another helper called get videos, which is going to be a function that returns a promise. What this promise is going to return is all of our videos.
Next, we'll add it to our exports by doing exports.getvideos=getvideos. Then, we'll switch back to our index.js file and make sure that we're importing it along with the other helper. I'll do get video by ID, and then also, get videos.
Finally, what we need to also include is the GraphQL list type from the GraphQL package. What we can do is use that inside of our field definition inside of our query type. Now to add the videos field, all we need to do is write out videos.
For its type, we'll say it's a new GraphQL list of the video type, we know that it's a collection of videos. Then for the resolve statement, we can return get videos or just shorten this to get videos, which will be executed whenever we're trying to resolve all of our videos.
Now, we can go and test this out by running our server. By doing nodeindex.js, we can switch over into our graphical editor. Now instead of querying for videos like before, we can query for videos. Then on videos, we can ask for the title and execute it.