Create & Interact with an AWS AppSync GraphQL API with AWS Amplify

nader dabit
Instructornader dabit
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 4 years ago

In this lesson we’ll create a new GraphQL API using the Amplify CLI and use the Amplify GraphQL API to query data from the new API & render it in our app. We’ll also look at how to perform mutations from the client

Instructor: [00:01] 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.

[00:18] 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.

[00:32] 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.

[00:53] 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.

[01:13] 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.

[01:34] In the API dashboard, you can click on Schema to take a look at the schema that's been auto-generated for us.

[01:48] 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.

[02:03] 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.

[02:34] To execute the mutation, we can click on the orange Play button.

[02:49] 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.

[03:27] 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.

[04:03] 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.

[04:37] 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.

[05:14] 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.

Abdullah
Abdullah
~ 5 years ago

Under componentDidMount the const is misspelled 'todoDate' in the transcript.

Felipe
Felipe
~ 5 years ago

Hello, in the transcript got an extra parenthesis this.state.todos.map((todo, i))

Ben
Ben
~ 5 years ago

After Amplify Push, getting new questions: ? Do you want to generate code for your newly created GraphQL API ? Choose the code generation language target [javascript, tuypescript, flow] ? Enter the file name pattern of graphql queries, mutations and subscriptions (src/graphql/**/*.js) ? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions (Y/n) ? Enter maximum statement depth [increase from default if your schema is deeply nested] (2)

Ben
Ben
~ 5 years ago

After Amplify Push, getting new questions:

? Choose the code generation language target  [javascript, tuypescript, flow]
? Enter the file name pattern of graphql queries, mutations and subscriptions (src/graphql/**/*.js)
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions (Y/n)
? Enter maximum statement depth [increase from default if your schema is deeply nested] (2)```
Big Human
Big Human
~ 5 years ago

++ to Ben's question

vj-raghavan
vj-raghavan
~ 4 years ago

Why are the code base completely different on every exercise ? It makes it difficult to follow and does not flow naturally from one exercise to the other.

Earl Damron
Earl Damron
~ 4 years ago

Ughh...I can't seem to get around this error:

Access to XMLHttpRequest at '<removed>/specs' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I sure don't recall deviating from the instructions at all.

Can I get any help maybe?

Markdown supported.
Become a member to join the discussionEnroll Today