Transcript from the "Creating a Schema & Database" Lesson
>> Steve Kinney: So we take a look at AppSync. We'll start by looking at it in the Dashboard, and then we'll go ahead and implement it in a clean slate version of our React app. So the master that we started earlier today on, we'll go back and implement that again with AppSync this time.
[00:00:14] Cool, so this is AppSync if you've never made an AppSync API before. If you have, you'll see some of your existing ones there. But this is the kind of tabula rasa for AppSync for the first time. So I'll go ahead and I'll hit this Create API button. And I have a few options here.
[00:00:32] I have some kind of pre-baked ones that kinda just show you how AppSync works. So there's an event app, a chat app, a tasks app, and a blog app. Cuz what demo of any Java script technology's not complete without a tasks app, right? You need to make a to do list.
[00:00:47] Let's just look at one. Let's look at the event app for a second, just to see what a fully fleshed-out AppSync API might look like. So these are events that have comments. And you can see that comments will be tied to an event ID. They'll have their own comment ID, so on and so forth.
[00:01:04] They'll have comment ID, so on, so forth. You can see, it's basically saying what are the properties and what are the types of those properties? And those exclamation points basically mean that it's required. Cool, so we have this idea of the comment. We have an event with the different properties there as well.
[00:01:18] We'll get into these comment connections in a second. Those are interesting ways to do pagination if you have a lot of something. When we make our API, we'll actually explore it a little bit more. So we have this idea of type. So what are the kind of nouns in our application?
[00:01:34] What kind of things do we have? At the same time, we also have mutations. How do we change things in our database? We can create new events. This is changing the data store. And then finally, we have queries, which are how do we get stuff out of our GraphicUL API?
[00:01:48] So in this case, get-event. We pass an ID. We'll get that event. List events is going to give us a list of events, so on and so forth. We also have some kind of built-in filtering in this case as well. So there's a lot that we kind of get.
[00:02:00] This is, again, a fully fleshed-out one. But I'm actually gonna show you how easy it is to get to this state even when you author it from scratch. So we'll go ahead, and we're gonna author it from scratch, and you'll see how quickly we can arrive at the station, if you will.
[00:02:15] So go ahead, and we'll name it Grudges. And we'll hit Create. Cool, so here, we get the API URL, API ID, Auth mode, and so on and so forth. If we need to download this configuration, you can see you can get it for a number of different platforms.
[00:02:34] In our case, we will go ahead and download the React version. We're gonna modify this a little bit, but we'll download it for now. Let's actually go ahead and look at Schema, right? And so before, with the event that was built in, you saw that we had a lot of stuff in there.
[00:02:50] There was a lot of various different, the ability to get stuff, change stuff, the different types. Let's look at something kind of that works for our application. We know what a grudge is, we've been working with grudges all day. We've been harboring them, we've been dwelling on them.
[00:03:06] We know that a grudge has an ID. And this ID type is basically just a unique string in this case. We know that there is a person that wronged us. That will be a string.
>> Steve Kinney: And we know that there is something that they did. That is also a string.
[00:03:26] And then, whether or not we have avenged that grudge, which is a Boolean.
>> Steve Kinney: And the exclamation point at the end is not to just be strong about our emotions, but also say that these are required fields. That it would need to have these properties in place in order for it to be a valid grudge.
>> Steve Kinney: Cool, and then we saw a little bit about queries. So we can do something like fetchGrudges or listGrudges, or something along those lines. This is actually not gonna matter too much. And you'll see in a second. Which will be an array of grudges that we'll get back.
[00:04:10] Cool, so with this kind of basics in place, we can actually hit the Save Schema button.
>> Steve Kinney: And it will go ahead and kind of figure out what our schema is. Now, we have a problem that's not dissimilar from a problem we had earlier today, which is a schema is cool and great.
[00:04:28] In the same way having an API is fine and wonderful. But unless you have a database to back it up, right, its not very good. Arguably, the opposite is true. Before we could create databases but unless we had an API in front of it, there was no way to get stuff in and out of the database.
[00:04:42] So the nice thing about AppSync is that it's pretty easy to create the DynamoDB databases behind the API. We have this fancy Create Resources button up here. And we can define a new type, which is very similar to what I just did. Or we can use an existing type, which is what I wrote in that previous one as we were talking.
[00:05:03] Actually, we want Grudge. Cool, and then it will figure out okay, I should probably make DynamoDB table that is a Grudge table. We'll say that the primary key is ID. That seems good. We're not gonna use a sort key for now. And we have the switch to automatically generate GraphQL.
[00:05:21] Remember all that fancy stuff I showed you with event before, and you're like I don't wanna write all that, I'm very lazy? I'm very tired right now, I don't wanna write all of that. Luckily, you don't have to. This is going to be, based on that type that we just created, all of the GraphQL that AppSync creates for us.
[00:05:35] Which is, again, very similar to all the things that we saw when it came time to look at the events earlier. And you can see, we've got the ability to Update, Delete, Create. We have the ability to Search and filter. We have that Grudge connection, which we'll talk about shortly.
[00:05:51] So this is very similar to the events that we had before. So Author from Scratch is a little bit disingenuous because while we selected Author from Scratch, AWS is doing a lot of the heavy lifting for us. So we'll go ahead and we'll scroll down. We'll hit Create.
[00:06:10] And you'll get this please do not refresh as it creates the Dynamo DB table. I have never tempted fate and tried to refresh despite oppositional defiance disorder. Cool, so now, we have this resource creation complete. And you can see, we have this brand new schema in place, which is much more robust than the previous schema.
[00:06:29] And just to kind of prove to you I can open up a new tab.
>> Steve Kinney: We can go ahead and check out, go to the Console.
>> Steve Kinney: So I'm just gonna go into the DynamoDB section of AWS, and just basically prove to both you and myself that a DynamoDB database was created.
[00:06:58] So I go to Tables, there is that GrudgeTable that we created earlier, right? So we defined a very simple GraphQL schema. We hit Save Schema. And then, we hit Create Resources. And a database was built for us under the hood. As we go and add stuff through GraphQL, this is the database that's going to be populated.
[00:07:17] So you can start with AppSynch, and if you decide later on that you don't want to use AppSynch or whatever, you have a just regular old DynamoDB database under the hood. So let's go back, and let's play around with this a little bit.
>> Steve Kinney: So we've got our resources, we've got our schema.
[00:07:32] Let's go into this Queries section. And we can begin to work with the schema a little bit.