ํด๋น ์์ ๋ DynamoDB ํ
์ด๋ธ์ Data Subscribe ํ๋ App Sync ์์ ์
๋๋ค.
2022๋
2์ 10์ผ ๊ธฐ์ค์ผ๋ก ๋์ ํ์ธ ๋ฐ ์์ฑ๋์์ต๋๋ค.
1. Amplify CLI ๋ค์ด๋ก๋ ๋ฐ ์ค์น (๋งํฌ)
window ๊ธฐ์ค, cmd ํน์ powershell ์์ ์๋ ๋งํฌ๋ฅผ ์คํ.
curl -sL https://aws-amplify.github.io/amplify-cli/install-win -o install.cmd && install.cmd
2. ํ๋ก์ ํธ์ ์ธํ ํ๊ธฐ
* ์์ ์ด Spring Boot ํ๋ ์์ํฌ์์ ์ด๋ฃจ์ด์ก๊ธฐ ๋๋ฌธ์ ๊ฒฝ๋ก๋ Boot ๊ธฐ์ค์ ๋๋ค.
ํ๋ก์ ํธ ์ธํ
cmd ํน์ powershell๋ก ํ๋ก์ ํธ ๋ฃจํธ์ ์ ๊ทผ ํ ์๋ ๋ช ๋ น ์คํ
amplify init
์ดํ ๋์ค๋ ์ ํ์ ์๋์ ๊ฐ์ด ์งํํ์์ผ๋ฉฐ, ๋ค๋ฅธ ๊ฒฝ์ฐ ์ํฉ์ ๋ง๊ฒ ์ ํํฉ๋๋ค.
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project acaasweb
The following configuration will be applied:
Project information
| Name: webTest
| Environment: dev
| Default editor: Visual Studio Code
| App type: android
| Res directory: app/src/main/res
? Initialize the project with the above configuration? No
? Enter a name for the environment dev
? Choose your default editor: Visual Studio Code
? Choose the type of app that you're building javascript
Please tell us about your project
? What javascript framework are you using none
? Source Directory Path: src/main/resources/static/js/graphql
? Distribution Directory Path: dist
? Build Command: npm.cmd run-script build
? Start Command: npm.cmd run-script start
Using default provider awscloudformation
? Select the authentication method you want to use: AWS profile
์ ์ ๊ฒฝ์ฐ, AWS profile์ ๋ฐ๋ก C:\Users\{userName}\.aws ์ credential ํ์ผ๋ก ์ ์ฅ๋์ด ์์ต๋๋ค.
App Sync ์ ์์ฑ๋ API ์ฐ๊ฒฐํ๊ธฐ
AppSync์ ์ ์ํ์ฌ ์ฐ๊ฒฐํ๊ณ ์ ํ๋ API๋ฅผ ์กฐํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ํ๋ฉด์ด ์์ต๋๋ค.

๋ด์ฉ ์ค, codegen ๋ถํฐ ์คํํฉ๋๋ค.
amplify add codegen --apiId {API_KEY}
์ด ๋ช ๋ น์ ์คํํ๋ฉด, ํด๋น API์์ ์ฌ์ฉํ๋ ์ฟผ๋ฆฌ๋ค์ ์ ์ธํ ๋ณ์๋ค์ ๊ฐ์ง๋ js ํ์ผ์ ์์ฑํฉ๋๋ค.
์๋๋ ํ์ผ์ ์์ฑํ ๊ฒฝ๋ก๋ฅผ ์ง์ ํ๋ ์ต์ ์ด๋ค. ์ ๋ช ๋ น์ ์คํํ๋ฉด ๋์ต๋๋ค.
โ Getting API details
Successfully added API ACaaS_AppSync_Service_Dev to your Amplify project
? Choose the code generation language target javascript
? Enter the file name pattern of graphql queries, mutations and subscriptions src/main/resources/static/js/graphql/**/*.js
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions Yes
? Enter maximum statement depth [increase from default if your schema is deeply nested] 2
โ Downloaded the schema
โ Generated GraphQL operations successfully and saved at src\main\resources\static\js\graphql
codegen ์ค์ ์ด ์๋ชป๋ ๊ฒฝ์ฐ, ์๋ ๋ช ๋ น์ ์ด์ฉํด ์์ ์ด ๊ฐ๋ฅํฉ๋๋ค.
amplify configure codegen
? Choose the code generation language target javascript
? Enter the file name pattern of graphql queries, mutations and subscriptions src/main/resources/static/js/graphQL/*.js
? Enter maximum statement depth [increase from default if your schema is deeply nested] 2
์ ์์ ์ ๋ง์น๊ฒ๋๋ฉด ์๋์ ๊ฐ์ด ์ค์ ํ ๊ฒฝ๋ก์ ํ์ผ์ด ์์ฑ๋ฉ๋๋ค.

3. ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
๋ฐ์ดํฐ ๊ฐ์ ธ์ค๋ ๋ฐฉ์์ Graph QL์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ฉฐ ๋ค์ ๋งํฌ์์ ๊ด๋ จ ๋ฌธ์๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
1. Amplify.js ์ถ๊ฐ
script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/aws-amplify/4.3.15-unstable.1/aws-amplify.js"></script>
2. ์์ค ์์ฑ ( jsํ์ผ์ ์์ฑ ํ, html ์์ import ํจ )
import awsconfig from './aws-exports.js';
import * as queries from './queries.js';
import * as mutations from './mutations.js';
import * as subscriptions from './subscriptions.js';
const Amplify = aws_amplify.API;
const graphqlOperation = aws_amplify.graphqlOperation;
// Considering you have an existing aws-exports.js configuration file
Amplify.configure(awsconfig);
/* ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ */
const list = await Amplify.graphql({ query: queries.{queries_get_method}, variables: { deviceId: 'DD000000002010', eventType: '1' } });
console.log("โฅโฅโฅโฅโฅ Dynamo Data get list (manual) โฅโฅโฅโฅโฅ");
console.log("deviceId : " + list.data.getACaaS_AppSync_dev.deviceId);
console.log("eventType : " + list.data.getACaaS_AppSync_dev.eventType);
console.log("โฅโฅโฅโฅโฅ Dynamo Data get list (manual) โฅโฅโฅโฅโฅ");
/* ๋ฐ์ดํฐ ๊ตฌ๋
*/
// Subscribe to creation of Todo
const subscription = Amplify.graphql({ query: subscriptions.{subscriptions_on_create_method}, variables: { deviceId: 'DD000000002010', eventType: '3' } }
).subscribe({
next: ({ provider, value }) => {
console.log("โฅโฅโฅโฅโฅ Subscribed Data : " + { provider, value } + " โฅโฅโฅโฅโฅ")
console.log(provider);
console.log(value);
},
error: error => console.warn(error)
});
/* ๋ฐ์ดํฐ ๊ตฌ๋
์ ์ง */
// Stop receiving data updates from the subscription
//subscription.unsubscribe();
/* ๋ฐ์ดํฐ ์ธํ
ํ๊ธฐ */
/* const test_data = {
deviceId: 'DD000000002015',
eventType: '2'
};
const create = await Amplify.graphql({ query: mutations.{mutations.create_data_method}, variables: { input: test_data } });
console.log("โฅโฅโฅโฅโฅ Dynamo Data create data (manual) โฅโฅโฅโฅโฅ");
console.log("deviceId : " + create.data.{mutations.create_data_method}.deviceId);
console.log("eventType : " + create.data.{mutations.create_data_method}.eventType);
console.log("โฅโฅโฅโฅโฅ Dynamo Data create data (manual) โฅโฅโฅโฅโฅ"); */
๋๊ดํธ๋ก ์ฒ๋ฆฌ๋ ์์ญ์ codegen์ผ๋ก ์์ฑ๋ js ํ์ผ ๋ด์ ์๋์ผ๋ก ์์ฑ๋์ด์ ธ ์๋ ๋ฉ์๋ ๋ค์ ์ด๋ฆ์ ๋๋ค.
๋ฐ์ดํฐ ๊ตฌ๋ ์ ๊ฒฝ์ฐ, Dynamo DB ์ฐ๋๋ AppSync๋ผ๋ฉด ๋ฐ์ดํฐ๊ฐ ์์ฑ๋์ Dynamo DB ํ ์ด๋ธ์ ๋ค์ด์ค๋ ์๊ฐ ์ด๋ฒคํธ๊ฐ fire ๋์ด Data Subscribe ํ๋ ์ชฝ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ ์์ต๋๋ค.
๊ตณ์ด Dynamo DB์์ ์ธ๋ฑ์ค๋ฅผ ์์ฑํ์ง ์์๋ AppSync ์์ ํ ์คํธ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ ค ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ ์๋ ์์ผ๋ ์ฐธ๊ณ ํ์๋ฉด ๋ฉ๋๋ค.

'๐AWS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ECR] Spring Boot + JSP ํ๋ก์ ํธ JIB๋ก ECR ์ ๋ก๋ํ๊ธฐ (0) | 2022.03.23 |
---|---|
[App Sync] Java ์์ AppSync Api Request ๋ณด๋ด๋ ์์ . (0) | 2022.02.17 |
[DynamoDB] ํํฐ์ ํค/์ ๋ ฌ ํค/ํํฐ ์ ๋ฆฌ (0) | 2021.09.24 |
AWS CLI 2.0 ECR get-login-password ์ธ์ฆ ๋ฌธ์ (0) | 2021.07.07 |
๋๊ธ