Right now, we have this GraphQL schema that's being defined by a single query type, and inside of this query type, we have a single field called video, but we're resolving this field with a single object in a resolve statement.
While this is great for our examples, it's way more likely that we'll have a collection of videos like we have defined down here. Then when we're querying for a specific video, we want to get that video by its ID.
In GraphQL, we're actually able to pass arguments to fields by using the Args key. Inside of the Args, we give it the name of the argument, which in this case would be ID, its type, which would be GraphQL ID, and then a description, which in this case would be the ID of the video.
Now, when we go and run our server using node index.js and we actually go and query for that video field, we can see that we can add an argument right after the term video. The argument that we can pass in is ID and we'll pass in the ID of the video that we would want.
When we execute that, we still get the same video. Let's actually go and plug in some more diverse data to demonstrate how we could query by ID and resolve that in our resolve statement.
Let's hop back into our terminal and actually create a new directory. Now, we're going to have one called source/data and we'll just add a file inside of there called index.js.
Next up, we'll go and grab all of the video information that we have in this file, and we'll cut and paste it inside of that data/index.js file.
Finally, let's add a helper function called GetVideoByID. This is going to be a function that takes an ID and will return a new promise.
To implement this, we're going to go through and get the video after filtering through all the videos. For each video, what we're going to do is check to see if the current video ID matches the given ID. At the end, we'll just resolve with the video.
Finally, let's go and use exports.GetVideoByID to export our function. Then inside of our index.js file, let's go to the top and do const and GetVideoByID is equal to require, go into Source, and then Data.
Now that we have this request to help simulate fetching a video, let's go and update our resolve statement. Now, instead of resolving with a static object, instead, what we can do is write out our resolve as a function.
The first argument we don't care about, but the second one will contain all of the arguments that are being passed in to our resolve statement for our field.
We can use that inside of our function by doing return GetVideoByID, which will return a promise. Then, we'll pass in the argument.ID, which will be the specific ID for the video that we want.
Now, let's go into our command line and just restart our server by doing node index.js and then switching to our Chrome window and run the same request one more time.
Now, we get a video for the ID of A. We can change it to the ID B and make the same kind of request, and we'll get the other video. Now, we're fetching our videos by the given ID.