Instructor: To create an AWS AppSync API, we can run the command amplify add api. For the type of service, choose GraphQL. Next, we're prompted to give the API a name. I'll give mine a name of amplifyTodoApp.
For the authorization type, we'll choose API key. When we're asked if we have an annotated GraphQL schema, we'll choose No. If we'd like to have a guided schema creation, we'll choose Yes.
For what best describes our project, we're going to choose a single object with fields. Next, we're given an option to go ahead and edit the schema. Choose Yes. We're given an example todo GraphQL schema. We'll take the existing schema and add a new property of completed and then save the file.
Once the file's been saved, jump back to the command line and press Enter. Now the resources have been saved locally and we can run amplify push to create the resource in our account.
Once amplify push has successfully completed, open the AWS console. From the AWS console, search for "AWS AppSync" so we can go ahead and take a look at the new API that we just created.
In the API dashboard, you can click on Schema to take a look at the schema that's been auto-generated for us.
Next, click on Query so we can begin executing queries and mutations against the API. The first thing we'd like to do is create a new todo. We'll create a mutation called create. To view the API documentation, you can click on the Docs link on the right side of the screen.
When we click on Mutations, we see that we have three different types of mutations, createTodo, updateTodo, and deleteTodo. createTodo takes an input with a name, description and completed properties.
To execute the mutation, we can click on the orange Play button.
Now that we've created a couple of items, let's perform a query. We'll perform the listTodos query that returns an items array. We'll then return the ID, the name, the description, and the completed values for each todo in the array.
Now that we've populated our API with a little bit of data, let's query for the data from our React app. The first thing we'll do is that we'll import API and graphqlOperation from AWS Amplify. Next, we'll define our listTodos query to return the ID, the name, the description, and the completed properties for every item in the todos array.
In the class, we'll create some initial state to hold the todos array and set it as an empty array. We'll create a componentDidMount life cycle method where we'll call API.graphql, passing in the listTodos graphqlOperation.
Once the data's returned from our API, we'll call this.setState, updating the todos array with the data returned from the API. In the render method, we'll map over the todos array, showing the todo name and the todo description.
Now we're ready to run the app. I'll go ahead and open the terminal and run npm start. If everything is working correctly, we should see the todos on our screen.