GraphQL
ํ์ด์ค๋ถ์ด ๋ง๋ ์ฟผ๋ฆฌ ์ธ์ด,
REST API์ ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๊ฐ๋ฐ
- ์ค๋ฒํจ์นญ, ์ธ๋ํจ์นญ
- ๋ฐฑ์๋ API๊ฐ ์์ ๋ ๋๋ง๋ค API ๋ช ์ธ์ ์์ ๊ณผ์ ๊ณผ ํ๋ก ํธ์๋ ์ฝ๋๊ฐ ์์ ๋ ์ ์๋ ๋ฌธ์
REST API vs GraphQL
REST API | GraphQL | |
ํธ์ถ | ์ฌ๋ฌ ์๋ํฌ์ธํธ ํธ์ถ (/users, /articles) | ๋จ์ผ ์๋ํฌ์ธํธ (/graphql) |
๋ฐ์ดํฐ ์์ฒญ | ํญ์ ๋์ผํ ๋ฐ์ดํฐ ๊ตฌ์กฐ (์ค๋ฒํจ์นญ) | ํด๋ผ์ด์ธํธ๊ฐ ์ํ๋ ๋ฐ์ดํฐ๋ง ์์ฒญ ๊ฐ๋ฅ |
๋ฐ์ดํฐ ์กฐ์ | CRUD (GET, POST, DELETE, UPDATE) | query(์กฐํ), mutation(์์ ) |
์๋ฌ ์ฒ๋ฆฌ | HTTP ์ํ ์ฝ๋๋ก ์๋ฌ ๊ตฌ๋ถ | errors ํ๋๋ฅผ ํตํด ์๋ฌ ์ ๋ณด ์ ๊ณต |
๋ฐ์ดํฐ ์์ฒญ/์กฐ์
REST API
: HTTP ๋ฉ์๋(GET, POST, PUT, DELETE)๋ฅผ ์ฌ์ฉ
- ํด๋ผ์ด์ธํธ ๋ฐ์ดํฐ ์์ฒญ
axios.get(`/api/users/${userId}`)
.then(response => setUser(response.data))
.catch(error => console.error(error));
axios.get(`/api/users/${userId}/friends`)
.then(response => setFriens(response.data))
.catch(error => console.error(error));
GraphQL
: ๋ฐ์ดํฐ ์กฐํ(Query), ์์ (Mutation)
query {
user(id: 1) {
id
email
name
friends {
name
}
}
}
mutation {
createUser(user: {id: 3, name: "John", email: "john@test.com"}) {
id
name
email
}
}
- ์คํค๋ง/ํ์ ์ ์: ์ด๋ค ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ณ ์ด๋ค ๋ฐฉ์์ผ๋ก ์์ฒญ์ ๋ณด๋ผ ์ ์๋์ง ์ ์
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type User {
id: Int!
name: String!
email: String!
friends: [Friend]
}
type Friend {
name: String!
email: String!
}
input NewUser {
id: Int!
name: String!
email: String!
}
type Query {
users(): [User]
user(id: Int!): User
}
type Mutation {
createUser(name: String!, email: String!): User
deleteUser(userId: ID!): Boolean
}
- ๋ฆฌ์กธ๋ฒ (resolvers): GraphQL ์๋ฒ์์ ์ฟผ๋ฆฌ, ๋ฎคํ ์ด์ ์ ์ฒ๋ฆฌํ๋ ํจ์
const dummyUsers = [{ id: 1, name: 'kim', email: 'kim@test.com', friends: [{ name: 'Lee', email: 'lee@test.com'}, { name: 'Pack', email: 'pack@test.com' }] }, { id: 2, name: 'Lee', email: 'lee@test.com', friends: [{ name: 'kim', email: 'kim@test.com', friends: [] }}]
const resolvers = {
Query: {
users: () => {
return dummyUsers
},
user: (_: any, { id }: { id: number }) => {
const user = dummyUsers.find(user => user.id === id)
return user
},
},
Mutation: {
createUser: (_: any, { user }: { user: NewUser }) => {
const newUser = {
...user,
friends: []
}
users.push(newUser)
return newUser
},
deleteUser: (_: any: unknown, { id }: { id: number }) => {
dummyUsers = dummyUsers.filter((user) => user.id !== id)
return true
},
},
User: {
friends: (user: User) => {
return user.friends;
},
},
}
"Apollo-client", GraphQL API์ ํจ์จ์ ์ผ๋ก ์ํธ์์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
React ์ ํ๋ฆฌ์ผ์ด์ ์์ GraphQL API์ ์ฝ๊ฒ ์ํธ์์ฉ ๊ฐ๋ฅ (๋ฆฌ์กํธ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ Redux๋ฅผ ๋์ฒด ๊ฐ๋ฅ)
npm i graphql @apollo/client
- ์ค์
import { ApolloClient } from "@apollo/client";
import { ApolloProvider } from '@apollo/client/react';
const client = new ApolloClient({
uri: "https://localhost:8001",
});
ReactDOM.render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById('root')
);
- ์ฌ์ฉ
import { gql } from '@apollo/client';
const getUser = gql`
query user($id: Int!) {
user(id: $id) {
id
name
friends {
id
name
email
}
}
}
`;
- ์๋ต ๋ฐ์ดํฐ
{
"data": {
"user": {
"id": 1,
"name": "Kim",
"email": "kim@test.com",
"friends": [
{ "name": "Lee" },
{ "name": "Park" }
]
}
}
}
API ํ ์คํธ์ ๋ช ์ธ
REST API | GraphQL | |
ํ ์คํธ ๋๊ตฌ | Postman | GraphQL Playground |
API ๋ช ์ธ | Swagger | GraphQL Playground |
GraphQL Playground
GitHub - graphql/graphql-playground: ๐ฎ GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs
๐ฎ GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration) - graphql/graphql-playground
github.com
- ํ ์คํธ & ๋ช ์ธ ์์
์๋ฌ ์ฒ๋ฆฌ
REST API
: HTTP ์ํ ์ฝ๋ (400, 500 Error)
GraphQL
: errors ๋ฐฐ์ด์ ์๋ฌ ์ ๋ณด ์ ๊ณต
๊ฐ์ธ์ ์ผ๋ก ์ง์ ์ฌ์ฉํด ๋ดค์ ๋ GraphQL ๋ถํธํ๋ ์
- ํ๋ก ํธ์๋ ์ฝ๋๊ฐ ๊ธธ์ด์ง ์ ์๋ค. ํ์ํ ํ๋๋ฅผ ๋ชจ๋ ์์ฑํด์ผ ํจ
- ๋ฆฌ์กธ๋ฒ ๊ด๋ฆฌ: ํ๋ก์ ํธ๊ฐ ์ปค์ง์๋ก ๋ฆฌ์กธ๋ฒ ๊ด๋ฆฌ๊ฐ ์ด๋ ต๋ค๋ ์ . ํ๋ก์ ํธ๊ฐ ๋ชจ๋ ํ๋๋ฅผ ํ ๊ณณ์ ์ ์ํ๋ฉด ๊ด๋ฆฌ๊ฐ ํ๋ค๊ธฐ ๋๋ฌธ์ ๋ณดํต ๋ชจ๋ ๋จ์๋ก ๋ฆฌ์กธ๋ฒ๋ฅผ ๋๋ ์ ๊ด๋ฆฌํฉ๋๋ค.
- ๋ฆฌ์กธ๋ฒ ๋ค์ด๋ฐ.. ํ๋ก ํธ์์ ์ด๋ค ๋ก์ง์ ์ฒ๋ฆฌํ๋ ๊ฑด์ง ๋ฆฌ์กธ๋ฒ ๋ค์์ ํตํด ํ์ ํ๋ฏ๋ก ๋ช ํํ ๋ค์ด๋ฐ์ด ์ค์.. ๊ทธ๋์ ๋ฆฌ์กธ๋ฒ ๋ค์์ด ๊ธธ์ด์ง๋ ๊ฒฝ์ฐ๋ ๋ง์
'๊ฐ๋ฐ ๊ณต๋ถ > ์น ์ง์ ์ฐฝ๊ณ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Node.js ๊ตฌ์กฐ์ ๋์์๋ฆฌ (0) | 2024.11.14 |
---|---|
JWT (JSON Web Token) ๊ฐ๋ (0) | 2024.11.06 |