The underlying AWS mobile CLI used in this course has changed it's API. If you're starting to learn AWS, you may want to try the latest course. We now recommend you take the AWS for Front-End Engineers (ft. S3, Cloudfront & Route 53) course.
Transcript from the "GraphQL Overview" Lesson
>> Steve Kinney: So before we talk about AppSync, which is GraphQL as a service on AWS. We should probably have enough GraphQL now, so know like, what is GraphQL, right? That's probably helpful for understanding why AppSync might be really cool. We're not gonna go super in depth into GraphQL, it's called enough GraphQL to be dangerous for reason.
[00:00:21] So GraphQL is a data query language developed by, and used by Facebook. Which is like, that I guest just like an appeal to authority, right? Well, does it work in production? Apparently. So, there's a few kind of basic terminology that we need to know. Queries, get you stuff from the database.
[00:00:42] Mutations, change stuff in the database. Subscriptions, subscribe you to changes in your database. And the schema, is what your data look like. It's basically your API contract, typo. Alright, so GraphQL, and this is a GraphQL strongly typed. This is like a thing that might be in our GraphQL database.
[00:01:04] So you can see that, anything with an exclamation point at the end is a required field. Anything without an exclamation point is an optional field. So here an ID, an ID is really just a string, but it has to be a unique string. Which makes sense for ID's, right.
[00:01:18] Some kind of protein is a string, bread is a string and the toppings is an array of strings, right. So this defines, what the sandwich is. Now, we can also define like, what the queries are. All right, so getting an individual one, you give it an ID and you're gonna get back one.
[00:01:37] You can say like, okay I wanna get all of them, you're gonna get an array of all of them. Right, it makes, you're basically defining the kind of like. How the schema of your database looks like, what the API contract is going to be. And then, there's a series of resolvers that figure out what we do and what the client like when we get back from the client database.
[00:01:56] What's really cool about this is, is basically, you just define everything about your data. And then clients, also known as like, in our case, front end engineers, can tell you what they need. Right, rather than the constantly that I have to do sometimes which is, back in engineer, I made an API please?
[00:02:16] Right, like okay. So I have to make a new rest end point or like worse. Is like sometimes, like okay, I have to hit like seven different end points to get all the information that I need. This allows to say, here's what I need, you send it off to the GraphQL service, right?
[00:02:30] It hands it to the resolvers, it goes ahead and gets you all of the different like. Figures out the payload to give back to you, and so if your needs change, like based on your UI. You change your GraphQL query, you get back different stuff, which is kinda cool.
[00:02:45] So this is a query, for getting one thing, getting all the things, so this is how we do a mutation, right. This again, this is not actually how it happens in the database, right, cuz that would be really cool if you just wrote that. I mean, let me be really honest, that's like it is kinda really just that.
[00:03:02] But there is some like magic under the hood that we just go and like, and don't think about. So here we say a mutation will take a input, and return back a new item. And a query, we can go get an individual one. This is actually, we defined what a query was before.
[00:03:19] This is actually, querying the database. So here we're gonna get all the different properties of that, but let's say, I only need the ID of the protein. I just ask for those, and the API's only gonna return me those properties. So before we define a query, this is actually queries the data base.
[00:03:36] So in this case we say, okay I want to get the sandwich with this giving ID and here is the properties that I want. You only need a few properties, you don't find just the one that you need and it would just give you those. So yeah, the kind of the consumer of the API, has a lot more control about what they need to get back to create the UI.
[00:03:55] Than in traditional rest API's, where it's like, okay you hit this endpoint. Here's everything, filter out the things you need, manipulate the data as you need it for the UI, so on and so forth, so it's very cool. Here is how we like, this is adding something to the database, you'll notice that you can see like, createBanhMi twice.
[00:04:15] The first one is kind of, think about it in java script, you can have anonymous functions and named functions. We're just naming this mutation, that helps some client libraries cache what's going on. Really what's happening in the second one is in that schema before, when we created a mutation, we said what is it like.
[00:04:30] What does it mean to create one? So here we have the input of all the data that's gonna go into it, and then what we get back from the database. So if we only needed the ID back, we could just say like, okay, only give me the ID.
[00:04:40] So before when we had those dynamo DV databases, what they called API. And we weren't getting back the ID and we didn't like the API. This gives us a lot more control on our end, to define what exactly we need from any given endpoint. What we wanna get back when we create something, update something, delete something, so on and so forth, we get that control.