This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Create an Input Object Type for Complex Mutations

2:29 JavaScript lesson by

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type.

Instead of having this mutation field where each of the arguments correspond to some field that we want to create, we can use something called an input type to capture all of these arguments in a single object type and be able to query for these more complex related types.

What we can do is get rid of all of these arguments and replace them with a single one called video and the type of this will be a new GraphQL non-null and we'll call the argument here a video input type.

This type is going to be built off of something called a GraphQL input object type. I'll say const. Video input type is going to be equal to a new GraphQL input object type. We'll make sure to import that at the top of our file. We'll do GraphQL input object type, then we'll go back to our definition here and the name is going to be video input.

Then we have some fields that we want to put in there as well. In this case the fields are just going match exactly what we had before, as arguments inside of that mutation field.

Now that we have our video input type defined and we're using that as our argument type inside of CreateVideo, and we've updated our resolve statement now to say Args.Video, instead of just Args, since video here will be the object representing all of the fields that we're interested in, let's go and actually try this out in graphical.

Let's go and run our server using node index.js. Inside of our graphical editor we'll just refresh it really quick. Inside of our docs and inside of mutation, we can see that CreateVideo now has that video input as the input arguments for the mutation.

To actually write out this mutation we would say mutation, give it some kind of name -- in this case it's M. We'll have CreateVideo, which takes in a video. What this will look like is we'll have a title once again, so we'll say fu, duration, 300.

We'll also have release, which will be false. Then we'll have these curly braces at the end and we'll just query for the ID and the title. We'll run our mutation and there we go.

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