This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Use GraphQLNonNull for Required Fields

2:05 JavaScript lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

Avatar
IT

i get error, GraphQLNonNUll is not a constructor. This video is wrong.

In reply to egghead.io
Avatar
Josh Black

Hi there! Sorry to hear about the issue you're experiencing.

Unless there's been a significant change recently, GraphQLNonNull should still be a type that's exported from the graphql module and can be used as a constructor.

For reference, you can visit the graphql site to verify the expected behavior here.

You can also check the graphql repo to verify that GraphQLNonNull is an export here.

Hope that helps!

In reply to IT

For the queryType 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.

index.js

const queryType = new GraphQLObject({
  name: 'QueryType',
  description: 'The root query type.',
  fields: {
    video: {
      type: videoType,
      args: {
        id: {
          type: GraphQLID,
          description: 'The id of the video',
        },
      },
      resolve: () => new Promise(...),
    },
  },
});

But if we go and run this server using $ node index.js. Then, go into our GraphiQL 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.

GraphiQL Null Returned

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 GraphQLNonNul 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.

index.js

const queryType = new GraphQLObject({
  name: 'QueryType',
  description: 'The root query type.',
  fields: {
    video: {
      type: videoType,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLID),
          description: 'The id of the video',
        },
      },
      resolve: () => new Promise(...),
    },
  },
});

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 \"ID!\" is required but not provided.".

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: 'a' and we run our query once more, we'll get back the expected result.

Expected Result

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?