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.
[00:01] 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.
[00:11] 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.
[00:35] In order to make this ID argument required, we can use a part of GraphQL known as a GraphQLNonNull type.
[00:45] 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.
[01:13] 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.
[01:36] 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.
[01:52] 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.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!