Mutation ํบ์๋ณด๊ธฐ! ๐ ๏ธ
Mutation์ ๋ฐ์ดํฐ์ ์ํ๋ฅผ ๋ฐ๊พธ๋ ์์ ์ ์๋ฏธํฉ๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๋ณด๋ฅผ ์์ ํ๊ฑฐ๋ ์ถ๊ฐ, ์ญ์ ํ๋ ๋ชจ๋ ํ๋์ด ์ฌ๊ธฐ์ ์ํ์ฃ . GraphQL, React Query ๊ฐ์ ๊ณณ์์๋ mutation์ ์ค์ํ ๊ฐ๋ ์ด์์. ์ค๋์ ์ด mutation์ด๋ผ๋ ์์ฆ๋ง์ ๋ ์์ ํํค์ณ ๋ณผ ๊ฑฐ์์. ์ค๋น๋๋์? ๐
๐ Mutation์ด๋?
Mutation์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๊ธฐ ์ํ ์์ ์ด์์.
์ฌ๊ธฐ์ ๋ณ๊ฒฝ์ด๋:
- ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ธฐ
- ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ธฐ
- ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ธฐ
์ผ๋ฐ์ ์ผ๋ก REST API์ POST, PUT, DELETE์ ๋์๋๋ฉฐ, GraphQL์์ mutation์ด๋ผ๋ ํค์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃน๋๋ค.
๐ Mutation์ ์ธ๊ณ ํํ
RESTful API์์์ Mutation ๐งฉ
REST API์์ mutation์ ํํ CRUD(Create, Read, Update, Delete) ์์ ์ค Create, Update, Delete์ ํด๋นํด์.
์์ : ์ฌ์ฉ์ ์ถ๊ฐ
fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: 'Alice', age: 25 }),
});
์์ : ์ฌ์ฉ์ ์ ๋ณด ์์
fetch('https://api.example.com/users/1', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ age: 26 }),
});
์์ : ์ฌ์ฉ์ ์ญ์
fetch('https://api.example.com/users/1', {
method: 'DELETE',
});
GraphQL์์์ Mutation ๐งฉ
GraphQL์์๋ query๋ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ , mutation์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
์์ : ์ฌ์ฉ์ ์ถ๊ฐ
mutation {
addUser(name: "Alice", age: 25) {
id
name
age
}
}
์์ : ์ฌ์ฉ์ ์ ๋ณด ์์
mutation {
updateUser(id: 1, age: 26) {
id
name
age
}
}
์์ : ์ฌ์ฉ์ ์ญ์
mutation {
deleteUser(id: 1) {
success
}
}
GraphQL์์๋ mutation์ด ์๋ฒ์์ ์ํ๋ฅผ ๋ณ๊ฒฝํ๊ณ , ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ ์๋ ์์ด์. (REST API์ ๋ค๋ฅธ ์ค์ํ ํน์ง!)
๐ซ Mutation๊ณผ React Query: ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ ์ฐ๊ธ์ ์ฌ
React Query์์ mutation์ ์๋ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๊ธฐ ์ํ ๋๊ตฌ์์.
์๋ฅผ ๋ค์ด, ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐ, ์์ , ์ญ์ ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
React Query๋ก Mutation ์ค์ ํ๊ธฐ ๐ ๏ธ
1๏ธโฃ useMutation ํ ์ฌ์ฉํ๊ธฐ
React Query๋ useMutation์ ํตํด mutation์ ๊ด๋ฆฌํฉ๋๋ค.
import { useMutation } from 'react-query';
import axios from 'axios';
function addUser(userData) {
return axios.post('/users', userData);
}
function App() {
const mutation = useMutation(addUser);
const handleAddUser = () => {
mutation.mutate({ name: 'Alice', age: 25 });
};
return (
<div>
<button onClick={handleAddUser}>Add User</button>
{mutation.isLoading && <p>Loading...</p>}
{mutation.isError && <p>Error: {mutation.error.message}</p>}
{mutation.isSuccess && <p>User added successfully!</p>}
</div>
);
}
2๏ธโฃ onSuccess์ onError ์ฒ๋ฆฌ
React Query๋ mutation ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ ์์ ์ ์ฒ๋ฆฌํ ์ ์์ด์.
const mutation = useMutation(addUser, {
onSuccess: () => {
console.log('User added successfully!');
},
onError: (error) => {
console.error('Error adding user:', error);
},
});
๐ Mutation์ ์ฅ์
- ๋ช ํํ ์์ ๊ตฌ๋ถ: ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ์์ ์ ๋ช ํํ๊ฒ ๊ตฌ๋ถํด์ค๋๋ค.
- API ์๋ต ์ฒ๋ฆฌ: ์๋ฒ์ ์๋ต์ ๋ฐ๋ผ ์ฑ๊ณต/์คํจ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์์ด์.
- ์ฌ์ฌ์ฉ์ฑ: React Query, GraphQL ๋ฑ์ ํด์ ์ฌ์ฉํ๋ฉด, mutation ๋ก์ง์ ์ฌ์ฌ์ฉํ ์ ์์ด์.
โ ๏ธ Mutation ์ฌ์ฉ ์ ์ฃผ์์
1๏ธโฃ ๋ถ๋ณ์ฑ ์ ์ง
์ํ ๊ด๋ฆฌ์์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ ๋๋ ์๋ณธ์ ์์ ํ์ง ์๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ผ ํด์.
const newData = { ...data, age: 26 }; // ๋ถ๋ณ์ฑ ์ ์ง
2๏ธโฃ ์๋ฌ ํธ๋ค๋ง
mutation์ ์คํจํ ์ ์๊ธฐ ๋๋ฌธ์, ์ ์ ํ ์๋ฌ ์ฒ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.
3๏ธโฃ ์บ์ฑ
๋ฐ์ดํฐ๋ฅผ ์์ ํ ํ์๋ React Query ๋ฑ์ ์บ์๋ฅผ ์ ๋ฐ์ดํธํด UI์ ์๋ฒ ์ํ๋ฅผ ๋๊ธฐํํด์ผ ํด์.
๐ผ ์ค๋ฌด์์์ ์์ : ์ฌ์ฉ์ ๊ด๋ฆฌ
์๋ ์์ ๋ React Query์ useMutation์ ์ฌ์ฉํด ์ฌ์ฉ์๋ฅผ ์ถ๊ฐ, ์์ , ์ญ์ ํ๋ ์ค๋ฌด ์์ ์ ๋๋ค.
import { useMutation, useQueryClient } from 'react-query';
import axios from 'axios';
function App() {
const queryClient = useQueryClient();
const addUser = useMutation((newUser) => axios.post('/users', newUser), {
onSuccess: () => {
queryClient.invalidateQueries('users'); // ์บ์ ์
๋ฐ์ดํธ
},
});
const deleteUser = useMutation((id) => axios.delete(`/users/${id}`), {
onSuccess: () => {
queryClient.invalidateQueries('users');
},
});
const handleAddUser = () => {
addUser.mutate({ name: 'Bob', age: 30 });
};
const handleDeleteUser = (id) => {
deleteUser.mutate(id);
};
return (
<div>
<button onClick={handleAddUser}>Add User</button>
<button onClick={() => handleDeleteUser(1)}>Delete User</button>
</div>
);
}
๐ ๊ฒฐ๋ก
Mutation์ ์๋ฒ ๋ฐ์ดํฐ์ UI๋ฅผ ๋๊ธฐํํ๋ ๋ฐ ํ์์ ์ธ ๊ฐ๋ ์ด์์.
GraphQL, React Query์ ํจ๊ป ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ ๋ณ๊ฒฝ ์์ ์ ๋์ฑ ๊ฐ๋ ฅํ๊ณ ๊น๋ํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ต๋๋ค.
๐ท์ ์ค์ ๊ฐ๋ฐ์๊ฐ ๋์ด๋ด ์๋น!๐ท