Wrap GraphQL Mutation Arguments with a GraphQL Input Type

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet

When passing arguments to a GraphQL mutation, you can send them inline, or you can wrap them in an input type. In this lesson, we’ll create an input type for the addDay mutation which will allow us to pass an entire object as an argument.

Instructor: [00:00] First let's organize our arguments into an input type. Then I can highlight and delete all of the arguments sent to addDay and instead pass in the input addDayInput and we'll make it non-nullable. Now in the resolver all of these givens are going to listed under the input key. Here I can destructure date, mountain, and conditions.

[00:28] Now over in the playground, I'm going to create my mutation. I'll create a variable for input and set that to our addDayInput. I'm going to pass in input as an argument, and set that to the value of the variable. I'll request date, mountain, and conditions as the mutation response, and in the query variables panel I'm going to send these arguments under the input key. So the input object will have a field for date, for mountain, and for conditions.

[01:03] Now once I run this, we're going to see the addDay mutation execute as expected. Again, the input type is a wrapper around a set of arguments. We send that to the addDay mutation, and then we're going to destructure those values from under the input key because we're sending an entire input object each time we send the mutation.