This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Use Relay’s Input Object Mutations

    Josh BlackJosh Black

    In order to support mutations in Relay, there is a requirement that the GraphQL Server exposes mutation fields in a standardized way. This standard includes a way for mutations to accept and emit an identifier string, allowing Relay to track mutations and responses. In this video, we’ll learn how to use a helper available to us through graphql-relay to create Mutation fields that accept clientMutationId’s.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:01 In our existing schema we have this route mutation type and it has a single field called CreateVideo that has a video type. It takes in some arguments and then resolves with a CreateVideo method.

    00:12 While this worked in previous examples, in Relay there's a common pattern for mutations where they exist on the route field. The input for each mutation is a single argument called input. There are some other technical requirements as well to make mutations a little bit more predictable.

    00:32 We can update this existing mutation field to be relay-compliant by first grabbing a function from GraphQL relay. In this case we're going to get mutation with client mutation ID.

    00:45 Next up let's actually go and define our video mutation. I'll hop back down to the end of the file and go right above our mutation type. Here we'll say constant video mutation. It's going to be equal to calling mutation with client mutation ID and then passing in an object.

    01:04 This object takes in a couple of fields, namely name, which in this case will be AddVideo. It'll also take in input fields as an object, output fields as an object, then finally a method called mutate, and GetPayload, which is going to be a method that takes in some arguments. In our case we're going to return a promise.

    01:30 Input fields in this case will describe what fields are defined on our input object type, and so we can just grab the fields that we were defining on our VideoInput type. We'll grab all of these and then we can just put them inside of the input fields here. We'll go and delete the VideoInput type.

    01:50 Output fields in this case will correspond to what we can actually query on after the mutation. In this case we'll just write out video and the video field will have a type of VideoType. We can actually query on the created video after our mutation.

    02:07 The last part here is to fill out mutate and getPayload. Here the arguments being passed in will just correspond to whatever our input fields are. In this case we can just do promise.resolve and we can call our CreateVideo method and pass in the arguments.

    02:23 When we're done we'll get a single video and what we can do is resolve and we'll pass in an object. One of the fields will be video on there. If we have any kind of errors we'll just reject.

    02:37 The value that we end up returning or resolving from this method is what we're going to be able to pick out information from for these output fields. In our case if we want to be able to get the data under the video field here that's why we're resolving with an object and one of the keys on that object is video.

    02:57 Our last step here is to update our mutation type. Instead of having this config object for the CreateVideo field now we'll just have video mutation.

    03:09 Let's go see how our new mutation works in graphical. We'll run our server using node and then index.js. We'll bring in our graphical tool and just refresh. Now when we write our mutation we could write mutation and AddVideo query.

    03:26 For the input, it's going to be the AddVideoInput. In this case AddVideoInput corresponds to the name of our mutation, which is AddVideo and then it appends input at the end.

    03:41 We'll also need to make sure that this AddVideoInput part is required. We'll just add an exclamation mark after that to say that it's non-null. Inside of the mutation we'll just use CreateVideo, which will take in an input and in this case it'll be the variable input and then we can query on the video and get the title.

    04:00 Let's go and add the query variables now. We'll have the object. We'll have input, which has a title. In this case we'll just say VideoTitle. We'll also have duration, which will be 300. We'll have release, which will be false. Finally we'll have ClientMutationID, which in this case for us will be ABCD.

    04:25 Let's run our mutation now. We'll click execute and now we see that we have our video created and we can query on the resulting video and get the title. You can also write another query just to verify this. We could say AllVideos query.

    04:44 Here we'll query on videos and go through the edges and for each node we'll get the title. When we execute that we can see that our video was actually added to our list of videos.

    Discuss

    Discuss