Go back to 8base Academy
October 1, 2019

Working with GraphQL Queries in the API Explorer

Sebastian Scholl

* This is an automated transcript. Please excuse inaccuracies.

So, in this video we are going to go over how to write graph QL queries and we're going to do that in the API Explorer which you can find inside your ApS workspace.  


Let's jump right into it.  


So, if you want to follow along with this tutorial and actually write these exact same queries that I'll be writing you can do so by running through the first five steps of our QuickStart app. If you go to Docs.8base.com/gettingstarted/quickstart, you'll be able to find this tutorial and like I said just the first five steps and that will set up your workspace to have the exact same tables that I have defined as well as the same seed data that I imported.  


So, once you have your workspace set up or if you just want to follow along jump into the workspace and go over to the API Explorer, which you'll see in your left side menu. This is we're going to be for the rest of this video just to show you how to write two types of queries. One is a list query and the other is a table or a record specific query. So, one of the really cool things about 8base is that whenever you create or update a table we will generate or update the graph QL resources that you need to perform crud actions on that table- what does that mean? So, first off what I'm going to do is I'm going to delete all these comments right here so we can jump in and get started writing queries alright. So, I'm just going to type in query, give it curly braces. And before I get in because I'm a little bit familiar with the domain what I want to show you is if I jump over here to screen write and I open up the Documentation Explorer right. This is an introspection tool that will allow us to jump in and look at all the different types of queries mutations and subscriptions that we have defined and we can use when writing our graph QL statements.  


So, for right now in this application we have Brokers, we have properties, listings all these different types of table resources that you could think of as being part of let's say a real estate management application. So, I'm going to look at the Broker resource and you can see I have Broker and Broker list. These are the two types of graph QL queries that I have defined. So, Broker I'd have to pass an ID to because it's looking at a specific Broker. However, we're going to look at Broker list for now and query a list of results. So, you can see that out of the box you have filtering, you have ordering, pagination, all these different things that you can use to help shape the response that you want to get.  


So, if I jump in my query and I type in Broker you can see that we have a little bit of autocomplete going on here or Brokers list excuse me. And just to get going, I'm just going to say it okay for my Brokers list each item that it returns I want the ID and the nickname of the Broker so I'm going to run that query and the result pops up right here. And you can see we got a lot of duplicate nicknames for our Brokers. So, I'm just going to delete the nickname property and I'm going to jump in here and say that okay well for every Broker what I really want is, I want their listings and what I want for their listings is the count or the number of listings they have. And then for each listing item, I want- what's on that listing? I want the status and the price cool.  


I'm going to run that query and as you can see for every Broker, we have the Broker ID then for each Broker, we have their listings the number of listings they have as well as the status and price of that listing. So, what this just demonstrated is how easy it was for us to write a relational query and get back exactly the information that we're looking for and once again the power of this is knowing that you could actually do this from a client application. So, if you were developing a mobile app or any type of front-end application you would write this query there, execute it and then get back the information that you're looking for which is very different as I'm sure you can recognize and then what you would expect from a REST API.  


So, let's actually get a little bit more granular on this. So, here we're going to say, “Okay well we only want Brokers who…” then we're going to do a filter here. We want Brokers whose nickname contains- oh excuse me- so it contains and then just give it the letter A, right. So, in this example, we actually have a shorter list of Brokers because it's only returned ones whose name contains with the letter A, and we can actually add this type of filtering at multiple level of the relational query. So, for example here I wanted to do or let's say that I wanted to order the listings- let's do autocomplete- by price ascending. Boom! Do that! And now the prices have been ordered in ascending order.  


Once again, we are just changing and shaping this query as we want it to be and what's really cool is, we can even go a deeper level down into the query. What do I mean by that? So, right now we're starting the Brokers which has which are related to listings but listings also have relations. So, if for every listing we wanted to say, “Okay who is the seller of this listing?” And for that seller we want the user that that sort of belongs to- and again this is domain specific, but you get the idea. And we want their first name and their last name. Check that out. So, from the top level, we're getting our list of Brokers, for every Broker, we're getting all their listings, we're getting a aggregator that's counting them and then for each listing or that we have we're having the status the price that's an ascending order and then we are getting the seller which belongs to a user and the user’s first and last name- that is super cool.  


So, what we're going to do now right because I think that demonstrates a really good overview of some of the powerful things that you can do when you're creating lists queries right. And then of course as you know you can just dump into the Documentation Explorer over here and get a deeper or get a better look at all the different arguments that you can pass and how to use them. Let's just look at really quickly how you do a specific query for a specific record. So, if I were to jump back here, I'm just going to delete all this stuff inside and I were going to say, “Instead of looking for a Broker list, I just want a specific Broker and I'm going to pass it the ID as an argument.” And then on this Broker what I want to do is, have it or have it return the nickname of the Broker and I want to see the Brokers once again listings- build something similar to what we just had, but for a specific record. And what else do we got? For every listing that we have we want the seller before, we want the buyer and we want the buyers purchases and we want the number of purchases of the buyer had. Boom! Just like that right.  


So, once again we could just keep going and playing here, but I think this gives you a very good overview of all the different types of queries and how flexible the query and interface is when you're dealing with graph QL. I'm jumping over here just to the Broker query itself, so you can see that okay well these are the fields that you would have on the Broker as well as in the relationships that you can then filter, sort, page name, through whatever you need to do. but know that for every single table you create within your work space, all these types of calls are being automatically managed for you so that when you define them, they're ready to go right out of the box.  


I hope that you found this video valuable. if you're looking forward to staying up to date with future videos, please subscribe to the channel using the links below. And if you have any questions about some of the stuff that we went over, feel free to drop it in the comment section and I'll make sure to get back to you as soon as possible alright. Have a great rest of your day and looking forward to seeing you in future videos.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.