While certain fields in a GraphQL Schema can be optional, there are some fields or arguments that are necessary in order to either fulfill a query, or to provide a guarantee to people using the Schema that some field exists. In this video, we'll take a look at turning an argument in a NonNull argument by applying the GraphQLNonNull type in order to guarantee that the given argument is supplied in the query.
For the query type in our schema, we've defined some arguments. In this case, we have that single argument called "ID" that'll help us look up this video by an ID.
But if we go and run this server using node index.js. Then, go into our GraphQL editor and get rid of the argument here and run the query, we'll actually get "Null" as a response, because we're unable to fetch a given video for a given ID without the person querying giving that ID to us.
In order to make this ID argument required, we can use a part of GraphQL known as a GraphQLNonNull type.
To use that type, we'll go back into our editor and add in GraphQLNonNull as another part of our import from the GraphQL package, then we'll go to our query field, specifically the video. Instead of the type being GraphQLID, we'll say it is new GraphQLNonNull. We'll pass in a GraphQLID as the argument.
Now, if we go and restart our server using node index.js and head on over into our GraphQL tool and run that same kind of query again without the arguments and I'll get an error from GraphQL telling us that the field video argument ID of the given type GraphQLID is required, but we haven't provided it.
What this specific type signature is saying is that instead of being explicitly just a GraphQLID, we know that it is a GraphQLID that is non-null. In this case, when we're using it as an argument, it's a required field in order for us to resolve the query.
Now, if we go back to our query and add in "id of A" and we run our query once more, we'll get back the expected result.