⚠️ This lesson is retired and might contain outdated information.

Create an Input Object Type for Complex Mutations

Josh Black
InstructorJosh Black
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 2 years ago

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.

[00:01] 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.

[00:20] 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.

[00:37] 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.

[01:03] 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.

[01:17] 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.

[01:40] 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.

[01:59] 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.

[02:14] 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.

Albert Baliński
Albert Baliński
~ 6 years ago

The transcript doesn;t match the video :) We have:

mutation M {
  createVideo(video: {
    title: "Foo",
    duration:
  })
}

Should be:

mutation M {
  createVideo(video:{
    	title:"t",
    	duration: 1,
    	released:false
  }) {
    id, title
  }
}
Markdown supported.
Become a member to join the discussionEnroll Today