Go back to 8base Academy
October 1, 2019

Using GraphQL Mutations to Manage Model Relationships

Sebastian Scholl

* This is an automated transcript. Please excuse inaccuracies.

Hey there, in this video we're going to be showing how you can establish relationships between records while creating or updating them by using Mutations.  


If we were to look at the Mutations documentation on Docs.8base.com, we can see this relationship section which talks about, four different types of operations that can be used within a Mutation. Those are, create, connect, reconnect and disconnect. So, we're going to jump into the API Explorer and just play around with these to give you a great understanding of how they work and how you can use them in your own applications.  


So, apologies if this is too stereotypical, but for the sake of these examples we're going to be using a to-dos application so we have boards which has many to-dos and to-dos which belong to a board and then have a description and a status. All right so knowing the domain let's now jump over the API Explorer.  


Now that we're here I'm just going to delete the default comments and we're going to start immediately by writing a Mutation where we're going to be creating a board. So, board- create. We need to pass it some data and then on that we need to give it a name and so the name is just going to be example, “board”. And then as we can see the relationship to-dos gave us an option. So, if we do to-dos and then we pass that in array or actually we have to give that an object and then on that object, we can either specify, connect or create.  


So, connect would be the ability to add existing to-dos to this board after the board's been created. And create is going to give us the ability to once the board's been created also create some to-dos. So, let's do it with the create for this example. So, create and then if we want to, we can then say, now it's going to write because we have to pass it many to-do objects. So, then pass that object and now we're looking at the different types of fields that can be defined on a to-do. So, for example the description for this to do will be buy milk and cookies. And the status will be opened. Actually, let's look really quickly at our to-do and see which options we have pending. Pending is in the default value, so we can just leave it out. So, let's leave that out and let's say, okay so if this works what we want back is we went back to the name of the board. And then for the to-dos that were created we want for every to do the description and the status of that to do.  


So, first off let's do that okay. So, we create our board, example board and we also create our to-do item on that board by milk and cookies. And so, what's cool about that is in one single query we created two different records and establish the relationship between those records. So, if we go back to our to-do and we look at our data we can see the hey this to do buy milk and cookies, it belongs to the example board right.  


So, let's jump back to the API Explorer. I'm just going to comment this out for a second and run a quick query for our boards. So, board lists and for every board, we want its ID and let's just bring back its name, so we know which board it is. So, the example board here's the ID, so we're just going to save that over here and that's what we're going to use for the other examples.  


Okay so now that we've created this board, what we're going to do is we can uncomment it and we can say, “Hey, now we're going to do an update.” So, we're going to update this board and we're going to pass it- I think the name has to be unique, so I think it will be good here right. What we can just do instead, we can just specify the ID just so we know that we're doing this on the right board.  


So, now on an update let's see what options that we're given when it comes to the to-dos. So, I'm just going to delete eat create and look back. And so now we have, connect, disconnect, reconnect, create right. So, as I said earlier connect would be if we specify the ID of an existing to-do, we can now update it to be related to this board right. If we wanted to disconnect a specific to do, that would be the ability to specify to do that currently belongs to the board and remove that relationship. And reconnect is an interesting one to where let's say that, we have three different to do is related to our example board. And what we wanted to do was relate three new to-dos to this board, while dropping all the other ones reconnect would reconnect all the established relationships. So, use it carefully, but it becomes really helpful when that's the use case that you're going after right.  


So, let's just do this. So, let's say we're going to connect and we're going to pass it an array. And what we're going to do now is, we're going to go back to our data, we're going to look at the to-dos that we have. So, this to do it belongs to my first board. So, I'm going to look for the ID, copy the ID, go back to the API Explorer. Now I'm going to pass it an object with the ID set to that. Alright and so now we're going to run this again and we want the status in the description- cool. So, if I run this… all right so now it updated our example board we can see that it still has the original buy milk and cookies to do that we had given it and now it also update it so that we have the buy milk which has already been completed to do associated.  


So, let's do another one now to where now we're going to say, “Okay well we want to run that disconnect.” Run disconnect- exact same type of syntax- update that- alright and it returned now it's updated to where it's back to where originally started. Now what's cool here is that let's say, we wanted to do reconnect right. So, this was the ID of the one that we just removed. It still has the original to-do item attached to it; which is the buy milk and cookies. And remember you could this is an array so you could also always pass multiple to do is with their ID to it and do these come in bulk. So, if I execute this query, cool. See the removal relationship, established the new relationship with the to do item that we specified.  


So, as you can imagine in this to-do application, this is pretty straight for my this is valuable. However, in any type of application to where things are being created and they're highly depending on the relationships these types of Mutations become very valuable, because what you're not having to do is pull in different IDs and send IDs with records to relate them to whichever other records they're associated with. In one really clean action you're able to update your data as needed. So, I hope this was a helpful video on helping you understand how you can use relationships or establish relationships between records when using Mutations. If you have any questions, please leave them in the video comments below and looking forward to seeing you in future videos. Thank you.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.