[App Sync] Amplify ๋ฅผ ์ด์šฉํ•œ App Sync Data Subscribe.

๋ฐ˜์‘ํ˜•

ํ•ด๋‹น ์˜ˆ์ œ๋Š” DynamoDB ํ…Œ์ด๋ธ”์„ Data Subscribe ํ•˜๋Š” App Sync ์˜ˆ์ œ ์ž…๋‹ˆ๋‹ค.
2022๋…„ 2์›” 10์ผ ๊ธฐ์ค€์œผ๋กœ ๋™์ž‘ ํ™•์ธ ๋ฐ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

* Amplify ์‹ค์Šต๋งํฌ

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 ์—์„œ ํ…Œ์ŠคํŠธ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ  ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€