In this video, Emmanuel Henri goes through the basics of simple GraphQL queries with GraphiQL.
- [Instructor] So now let's take a look at simple queries, and then what we'll do is add some code into our server so we can actually do more complex queries. So let's go back to GraphiQL and remove the mutation. And what we'll do now is do a query. So let's go and do a query, and then let's do get contacts. And then, what do we want to return from get contacts? Do we want to have all the first names? All the last names? And so on and so forth, so you can enter these things in here.
So, first one that we like to have is first name, last name, and then maybe the company they're from. Once you run that query, or execute here, you're going to get list of all the items you entered in GraphiQL. And this is coming from the connection to the database. And basically resolve, by the resolvers. Now let's do something more complex. Let's go and write some code so we can do some more complex queries with some IDs and things like that, and we'll test it in the next video.
So let's go back to our code, and what I want to do first is go into the resolvers, so let's close all these other items that we have open, and let's open schema and resolvers, because those are two that we're going to need. So in the resolvers, right below get contacts, I'm going to create a brand new function that I'm going to call get one contact. And this one will take root again, and this is the syntax. And then it's going to take the ID of that particular contact.
And then function, and then everything else is very similar to what we've done right here. So we're returning a single contact, so let's go and copy most of this, and paste it here, and what we'll do is change just a few things. So, the first thing that we need to do is change the new contact save to find by ID. And let's go and change this to contacts dot find by ID, so the second one in the list.
And then instead of doing the error right away, what we want to pass is the ID, so we're passing the ID from here so we can find somebody with an ID. And then make sure that we have the variable, or argument, called contact. And then if error, reject error, else, resolve, contact. Like so. So let's save that. And now if we look into our server, we should have an error, because we need to also set this inside of our queries.
So we need to go here and then get one contact. And the syntax needs to be exactly the same as what you've done here, so get one contact. And then, this is how you pass arguments inside of a query, so you do ID, and it's expecting absolutely an ID so that makes it mandatory, so if you don't pass an ID then it's not going to resolve anything. Perfect, so save this.
Let's quickly check our terminal to make sure that we don't have any weird errors. Your server is running, so we are good to go. So we'll check all this in the next video.
- Setting up GraphQL
- Creating the initial schema and type
- Setting up simple queries
- Leveraging arguments and aliases
- Reading, updating, adding, and deleting items with GraphQL
- Posting data with mutations