๐ก Query๋ฅผ ์ด์ฉํด ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์์ฒญ
๐ก ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ๊ฒฝ์ฐ์๋ Mutation์ ์ด์ฉ
๐ก ๊ตฌ๋
(Subscription)์ด๋ผ๋ ๊ฐ๋
์ ์ ๊ณตํ๋ฉฐ ์ด๋ฅผ ์ด์ฉํด ์ค์๊ฐ ์
๋ฐ์ดํธ๋ฅผ ๊ตฌํ
-Query ๋๋ Mutation๊ณผ ๋ฌ๋ฆฌ, ๋ฐํ/๊ตฌ๋
(pub/sub) ๋ชจ๋ธ
-ํด๋ผ์ด์ธํธ๋ ์๋ฒ์ WebSocket์ ๊ธฐ๋ฐ์ผ๋ก ์ง์์ ์ธ ์ฐ๊ฒฐ์ ํ์ฑํ๊ณ ์ ์ง
๐ ํ๋(field)
์ฟผ๋ฆฌ์ ๊ฒฐ๊ณผ๊ฐ ์ ํํ๊ฒ ๊ฐ์ ๋ชจ์์ด์ด์ผ ํจ
//query
{
hero {
name
# ์ฃผ์ ์์ฑํ๋ ๋ฐฉ๋ฒ
friends {
name
}
}
}
//๊ฒฐ๊ณผ
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
โ
์ํ๋ ํ๋๋ฅผ ์ค์ฒฉํ์ฌ ์ฟผ๋ฆฌํ๋ ๊ฒ๋ ๊ฐ๋ฅ
๐ ์ ๋ฌ์ธ์(Arguments)
//query
{
human(id: "1000") {
name
height
}
}
//๊ฒฐ๊ณผ
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 1.72
}
}
}
โ
REST์ ๊ฐ์ ์์คํ
์์๋ ๋จ์ผ ์ธ์ ์งํฉ(์์ฒญ์ ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์ ๋ฐ URL ์ธ๊ทธ๋จผํธ)๋ง ์ ๋ฌ ๊ฐ๋ฅ
๐ ๋ณ๋ช (Aliases)
//ํ๋ฆฐ query
{
hero(episode: EMPIRE) {
name
}
hero(episode: JEDI) {
name
}
}
//query
{
empireHero: hero(episode: EMPIRE) {
name
}
jediHero: hero(episode: JEDI) {
name
}
}
//๊ฒฐ๊ณผ
{
"data": {
"empireHero": {
"name": "Luke Skywalker"
},
"jediHero": {
"name": "R2-D2"
}
}
}
โ
ํ๋ ์ด๋ฆ์ ์ค๋ณตํด์ ์ฌ์ฉํ ์ ์์
๐ ์คํผ๋ ์ด์ ๋ค์(Operation name)
//query
query HeroNameAndFriends {
hero {
name
friends {
name
}
}
}
//๊ฒฐ๊ณผ
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
โ
์คํผ๋ ์ด์
ํ์
์๋ query, mutation, subscription, describes ๋ฑ ์กด์ฌ
โ
๋ชจํธํ๊ฒ ํ๊ธฐํ์ง ์๋๋ก
๐ ๋ณ์(Variables)
query HeroNameAndFriends($episode: Episode) {
hero(episode: $episode) {
name
friends {
name
}
}
}
โ
$๋ณ์ ์ด๋ฆ: ํ์
ํํ
โ
$episode: Episode! ๋ episode๋ ๋ฐ๋์ Episode
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
type Character {
name: String!
appearsIn: [Episode!]!
}
โ
Character GraphQL ๊ฐ์ฒด ํ์
(ํ๋๊ฐ ์กด์ฌ)
โ
name / appearIn Character ํ์
์ ํ๋
โ
String ๋ด์ฅ๋ ์ค์นผ๋ผ ํ์
(๊ทธ์ธ ID, Int ๋ฑ)
๋จ์ผ ์ค์นผ๋ผ ๊ฐ์ฒด. ์ฟผ๋ฆฌ์์ ํ์ ์ ํ์ด ๋ถ๊ฐ๋ฅ
โ
! ์ด ํ๋๋ nullableํ์ง ์๊ณ ๋ฐ๋์ ๊ฐ์ด ์กด์ฌ
โ
[ ]๋ ๋ฐฐ์ด
์์ฒญ์ ๋ํ ์๋ต์ ๊ฒฐ์ ํด์ฃผ๋ ํจ์
const db = require("./../db")
const resolvers = {
Query: {
getUser: async (_, { email, pw }) => {
db.findOne({
where: { email, pw }
}) ...
...
}
},
Mutation: {
createUser: async (_, { email, pw, name }) => {
...
}
}
Subscription: {
newUser: async () => {
...
}
}
};