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