Skip to content

Commit 03efa6b

Browse files
Update amplifycli_react_tutorial.md
1 parent da458d8 commit 03efa6b

1 file changed

Lines changed: 93 additions & 2 deletions

File tree

amplifycli_react_tutorial.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ If you're using Windows, we recommend the [Windows Subsystem for Linux](https://
1212
- An AWS Account and credentials are necessary. If you're unfamiliar with finding credentials see the following [LINK](https://docs.aws.amazon.com/aws-mobile/latest/developerguide/aws-mobile-cli-credentials.html).
1313
- Ensure you have NodeJS installed
1414
- Examples below use `yarn` but `npm` works as well.
15-
- Download the Amplify CLI from [HERE](https://s3.amazonaws.com/amplify-cli-beta-3dd89264-8c7c-11e8-9eb6-529269fb1459/amplify-cli-0.1.0.tgz)
15+
- Download the Amplify CLI from [HERE](https://s3.amazonaws.com/amplify-cli-beta-3dd89264-8c7c-11e8-9eb6-529269fb1459/amplify-cli-0.1.0-8-8-18.tgz)
1616
- Navigate into the unpacked directory (usually called `package`) and run `npm run setup-dev`
1717
- Ensure you have [Create React App](https://github.com/facebook/create-react-app) installed and create a new project:
1818
```
@@ -116,10 +116,101 @@ Finally, modify the `render` method so that you can upload files and also view a
116116

117117
Save your changes and run `amplify publish`. Since you already pushed the changes earlier just the local build will be created and uploaded to the hosting bucket. Login as before if necessary and you'll be able to upload photos, which are protected by user. You can refresh the page to view them after uploading.
118118

119+
# Add GraphQL backend
120+
121+
Now that your application is setup, the final piece is to add a backend API with data that can be peristed in a database. The Amplify CLI comes with a **GraphQL Transformer** that converts annotated GraphQL schema files into the appropriate CloudFormation based on your data requirements. This includes options such as:
122+
- `@model` for storing types in Amazon DynamoDB
123+
- `@auth` to define different authorization strategies
124+
- `@connection` for specifying relationships between `@model` object types
125+
- `@searchable` to stream `@model` types into Amazon Elasticsearch
126+
127+
To get started run `amplify add api` and select `GraphQL`. When prompted choose `Amazon Cognito User Pools` and the project will leverage your existing Auth setup. Select **No** when asked for an `annotated schema` and **Yes** when asked about `guided schema creation`.
128+
129+
The guided creation will give some default schemas that are pre-annotated for you for learning purposes. The following steps take you through choosing `Single object with fields` but feel free to revisit these steps later in another project. If you choose this option you'll see the following annotated schema in your text editor:
130+
131+
```
132+
type Todo @model {
133+
id: ID!
134+
name: String!
135+
description: String
136+
}
137+
```
138+
139+
This is the GraphQL schema that will be deployed to AWS AppSync. If you're familiar with GraphQL you could rename/add fields & types, but note the client code would need to change as well. When you're ready press `enter` in the CLI and then `No` on the next question followed by `amplify push`.
140+
141+
After the deployment is done open your `App.js` again and update the import to include both the `API` category and `graphqlOperation` method:
142+
143+
```
144+
import Amplify, { Analytics, Storage, API, graphqlOperation } from 'aws-amplify';
145+
```
146+
147+
Add the following query and mutations in your code, **before** the `class App extends Component {...}` definition:
148+
149+
```
150+
const listTodos = `query listTodos {
151+
listTodo{
152+
items{
153+
id
154+
name
155+
description
156+
}
157+
}
158+
}`
159+
160+
const addTodo = `mutation createTodo($name:String! $description: String!) {
161+
createTodo(input:{
162+
name:$name
163+
description:$description
164+
}){
165+
id
166+
name
167+
description
168+
}
169+
}`
170+
171+
```
172+
173+
Now inside the `App` component add in the following two methods before the `render()` method:
174+
175+
```
176+
todoMutation = async () => {
177+
const todoDetails = {
178+
name: 'Party tonight!',
179+
description: 'Amplify CLI works!'
180+
};
181+
182+
const newEvent = await API.graphql(graphqlOperation(addTodo, todoDetails));
183+
alert(JSON.stringify(newEvent));
184+
}
185+
186+
listQuery = async () => {
187+
console.log('listing todos');
188+
const allTodos = await API.graphql(graphqlOperation(listTodos));
189+
alert(JSON.stringify(allTodos));
190+
}
191+
```
192+
193+
You can now make GraphQL calls from your application. Update the `render()` method so that it has the following buttons to invoke the mutation and query:
194+
195+
```
196+
render() {
197+
return (
198+
<div className="App">
199+
<p> Pick a file</p>
200+
<input type="file" onChange={this.uploadFile} />
201+
<button onClick={this.listQuery}>GraphQL Query</button>
202+
<button onClick={this.todoMutation}>GraphQL Mutation</button>
203+
<S3Album level="private" path='' />
204+
</div>
205+
);
206+
}
207+
```
208+
209+
Save the file and run `amplify publish`. After the backend is deployed you should be able to press the **GraphQL Mutation** button to enter data into the database and **GraphQL Query** to retrieve a list of all entries. You can validate this in the AWS AppSync console as well.
119210

120211
# Add REST API calls to a database
121212

122-
Now that your application is setup, the final piece is to add a backend API with data that can be peristed in a database. For this example we will use a REST backend with a NoSQL database. Run `amplify add api` and follow the prompts, select the **API Gateway** provider, giving your API a friendly name such as **myapi** or something else that you remember. Use the default `/items` path and select **Create a new lambda function**. Select the option titled **CRUD function for Amazon DynamoDB table (Integration with Amazon API Gateway and Amazon DynamoDB)** when prompted. This will create an architecture of Amazon API Gateway with Express running in a Lambda function that reads and writes to Amazon DynamoDB. You'll be able to later modify the routes in the Lambda function to meet your needs and update it in the cloud.
213+
For this example we will use a REST backend with a NoSQL database. Run `amplify add api` and follow the prompts, select the **API Gateway** provider, giving your API a friendly name such as **myapi** or something else that you remember. Use the default `/items` path and select **Create a new lambda function**. Select the option titled **CRUD function for Amazon DynamoDB table (Integration with Amazon API Gateway and Amazon DynamoDB)** when prompted. This will create an architecture of Amazon API Gateway with Express running in a Lambda function that reads and writes to Amazon DynamoDB. You'll be able to later modify the routes in the Lambda function to meet your needs and update it in the cloud.
123214

124215
Since you do not have a database provisioned yet, the CLI workflow will prompt you for this. Alternatively, you could have run `amplify add storage` beforehand to create a DynamoDB table and use it in this setup. When the CLI asks you for the Primary Key structure use an attribute named `id` of type `String`. Don't select any other options like Sort keys or GSIs.
125216

0 commit comments

Comments
 (0)