Graph QL(3) - typeDefs์ resolvers
๐์์ํ๋ฉฐ
์ ํ๋ธ ๊ฐ์๋ฅผ ๋ฃ๊ณ ๋ฐฑ์๋์์ ๋ง๋ค์ด ๋ ๋ด์ฉ์ readํ๋ ๊ฐ๋จํ ์ฑ์ ๋ง๋ค์ด๋ดค๋๋ฐ, ๋ฐฑ์๋ ์ค์ ๋ถ๋ถ์์ ์ดํด๊ฐ ๋ถ์กฑํ ๋ถ๋ถ์ ์ฑ์ฐ๊ณ ์ ํ๋ค.๐
โ typeDefs
typeDef(Type Definition)๋ GraphQL์คํค๋ง ๊ตฌ์กฐ๋ฅผ ์ ์ํ๋ค. ๊ฐ ํ๋๋ ์ง์ ๋ ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋๋ฐ, ํ๋์ ๋ฐํ ์ ํ์ scalar, object, enum, union, interface๊ฐ ๊ฐ๋ฅํ๋ค.
โก๏ธScalar types
GraphQL์ ๊ธฐ๋ณธ ์ค์นผ๋ผ ์ ํ์ ๋ค์๊ณผ ๊ฐ๋ค.
ํ์ | ์ค๋ช |
---|---|
Int | 32๋นํธ ์ ์ ์ซ์ |
Float | ์์์ ์ด ์๋ ์ซ์ |
String | ๋ฌธ์์ด (๋ฌธ์๋ค์ ๋ชจ์) |
Boolean | true ๋๋ false (์ฐธ ๋๋ ๊ฑฐ์ง) |
ID | ๊ฐ์ฒด๋ฅผ ๋ค์ ๊ฐ์ ธ์ค๊ฑฐ๋ ์บ์์ ํค๋ก ์ฌ์ฉํ๋ ๊ณ ์ ์๋ณ์. ๋ฌธ์์ด๋ก ์ ์ฅ๋์ง๋ง ์ฌ๋์ด ์ฝ๊ธฐ ์ํด ์ฌ์ฉ๋์ง๋ ์์. |
โก๏ธObject types
์ฒด ํ์ ์ ๊ฐ๊ธฐ ๋ค๋ฅธ ํ์ ์ ํ๋๋ค์ ๋ชจ์ ๋์ ๊ฒ์ผ๋ก ๋ ๊ฐ์ ๊ฐ์ฒด ํ์ ์ ์๋ก๋ฅผ ํ๋๋ก ํฌํจํ ์ ์๋ค.
1
2
3
4
5
6
7
8
9
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
- Book ํ์ : ์ฑ ์ ์ ๋ชฉ๊ณผ ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ง.
- Author ํ์ : ์ ์์ ์ด๋ฆ๊ณผ ์ ์๊ฐ ์ด ์ฑ ๋ค์ ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ง.
์ฆ, ์์ ๊ฐ์ด type์ด ์ ์๋์๋ค๋ฉด, ์ด๋ฌํ ๋ฐ์ดํฐ ํํ๋ฅผ ๊ฐ์ง ์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"title": "ํด๋ฆฌํฌํฐ",
"author": {
"name": "JK ๋กค๋ง",
"books": [
{
"title": "ํด๋ฆฌํฌํฐ์ ์ฃฝ์์ ์ฑ๋ฌผ"
},
{
"title": "ํด๋ฆฌํฌํฐ์ ๋น๋ฐ์ ๋ฐฉ"
}
]
}
}
โก๏ธThe Query type
Query ํ์ ์ ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์ ์์ฒญํ ์ ์๋ ์ฝ๊ธฐ ์์ ์ ์ง์ ์ ์ ์ ์ํ๋ค. ์ฆ, REST API๋ก ์๊ฐํด๋ณด๋ฉด, READ์ ํด๋นํ๋ ์์ ์ ํ ๋ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก, ์ฝ๊ธฐ ์์ ์ ํ์ํ ๋ด์ฉ(ํ์ )์ ๋ชจ๋ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
์๋์ ์ฝ๋๋ฅผ ์ดํด๋ณด์. Query ํ์ ์๋ books์ authors ํ๋๊ฐ ์๊ณ , ๊ฐ๊ฐ Book๊ณผ Author ๊ฐ์ฒด์ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { gql } = require("apollo-server")
const typeDefs = gql`
type Query {
books: [Book]
authors: [Author]
}
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
`
โก๏ธThe Mutation type
Mutation ํ์ ์ ๊ตฌ์กฐ์ ๋ชฉ์ ๋ฉด์์ Query ํ์ ๊ณผ ๋น์ทํ๋ค. Query ํ์ ์ด ์ฝ๊ธฐ ์์ (READ)์ ์ง์ ์ ์ ์ ์ํ๋ค๋ฉด, Mutation ํ์ ์ ์ฐ๊ธฐ ์์ (CREATE, DELETE, UPDATE)์ ์ง์ ์ ์ ์ ์ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
type Mutation {
addBook(title: String, author: Author): Book
}
์ฌ๊ธฐ์ addBook์ ์๋ก์ด ์ฑ ์ ์ถ๊ฐํ๋๋ฐ ์ฌ์ฉ๋๋ ๋ณ์ด(mutator)๋ฅผ ์ ์ํ๋ค. ์ด๋ ์ด mutator๋ ๋ ๊ฐ์ ์ธ์ title๊ณผ author๋ฅผ ๋ฐ์ Book ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
์ฌ๊ธฐ์ Book์ ์์์ ์ ์ํ type Book
์ ๊ฐ๋ฆฌํค๋ฉฐ Author ๋ํ type Author
ํ์
์ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
โ resolvers
GraphQL ์คํค๋ง๋ ๋ฐ์ดํฐ์ ๊ตฌ์กฐ์ ํ์ ์ ์ ์ํ์ง๋ง ์ค์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ฑฐ๋ ๊ณ์ฐํ๋ ์์ ์ Resolver๊ฐ ๋ด๋นํ๋ค. ์ฆ, Resolver๊ฐ ์์ผ๋ฉด ์ํด๋ก ์๋ฒ๋ ์ด๋ป๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์์ผ ํ๋์ง ์ ์ ์๊ณ ํด๋ผ์ด์ธํธ ์์ฒญ์ ๋ํ ์๋ต์ ์์ฑํ ์ ์๋ค.
์๋ ์์ ๋ฅผ ๋ค์ ์ดํด๋ณด์. ์ฌ๊ธฐ์ typeDefs
๋ฅผ ํตํด ์คํค๋ง๋ฅผ ์ ์ํ ๋ ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ์์ฒญํ๊ณ ๊ฐ์ ธ์ฌ์ง ๊ฒฐ์ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { gql } = require("apollo-server")
const typeDefs = gql`
type Query {
books: [Book]
authors: [Author]
}
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
`
๊ทธ๋ฆฌ๊ณ ์ค์ ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๋ ์ญํ ์ resolvers
๊ฐ ํ๊ฒ ๋๋ ๊ฒ์ด๋ค!
1
2
3
4
5
6
const resolvers = {
Query: {
books: () => books,
movies: () => movies,
},
}
โก๏ธmutation typeDefs์ resolvers
์ถ๊ฐ๋ก mutation์ ์ด์ฉํด ๋ฐ์ดํฐ๋ฅผ ์
๋ฐ์ดํธ ํด๋ณด์! ๋จผ์ ์๋์ ๊ฐ์ด type Book
๊ณผ type Mutation
์ ์ ์ํด์คฌ๋ค. ์ด๋ type Mutation
์ type Book
๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
const typeDefs = gql`
type Book {
id: Int
title: String
author: String
}
type Mutation {
addBook(id: Int, title: String, author: String): Book
}
`
์ด๋ ๊ฒ ๋ง๋ Mutation์์ addBook์ด๋ผ๋ mutator์ ์ด๋ป๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ์ ๋ฐ์ดํธ ํ ์ง์ ๊ดํด resolvers๋ฅผ ์์ฑํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
let nextBookId = 3
const resolvers = {
Mutation: {
addBook: (_, { title, author }) => {
const newBook = { id: nextBookId++, title, author }
books.push(newBook)
return newBook
},
},
}