๐ถ๐ป GraphQL๋ก WordPress๋ฅผ ๋์ด๋ฆฌ๋ค
WordPress๋ ๋ ๊ฑฐ์ CMS์ ๋๋ค. 17๋ ์ด์ ์ ์ ํ์ํ ์ด ์์คํ ์, ์ง๊ธ ๋ค์ ๋ง๋ ๋ค๋ฉด ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ์์ฑ๋์์ PHP ์ฝ๋๋ก ๊ฐ๋ ์ฐจ ์์ต๋๋ค.
GraphQL์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํ ํ๋์ ์ธ ์ธํฐํ์ด์ค์ ๋๋ค. ใ์ธํฐํ์ด์คใ๋ผ๋ ๋จ์ด์ ์ฃผ๋ชฉํด ์ฃผ์ธ์. GraphQL์ ๋ด๋ถ ๋ฐ์ดํฐ ์์คํ ์ด ์ด๋ป๊ฒ ๊ตฌํ๋์ด ์๋์ง๋ฅผ ์ ๊ฒฝ ์ฐ์ง ์๊ณ , ์ค์ง ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ๋ ธ์ถํ ๊ฒ์ธ์ง์๋ง ๊ด์ฌ์ ๋ก๋๋ค.
์ด ๋ ๊ฐ์ง๋ฅผ ๊ฒฐํฉํ๋ฉด ์ด๋ค ์ผ์ด ์ผ์ด๋ ๊น์? WordPress ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํ GraphQL ์ธํฐํ์ด์ค๋ ์ด๋ป๊ฒ ์ค๊ณํด์ผ ํ ๊น์?
๋๋ต์ ์ผ๋ก ๋ ๊ฐ์ง ์ ๋ต์ ์๊ฐํด ๋ณผ ์ ์์ต๋๋ค.
-
์ ํต์ ์กด์คํ๊ณ , WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ์ ๊ทธ๋๋ก ์ ์งํ๋ ๋งคํ์ ์ ๊ณตํฉ๋๋ค (์๋ ์ ๊ฑธ์ณ ์์ธ ๊ธฐ์ ์ ๋ถ์ฑ๋ ํฌํจํ์ฌ)
-
๊ธฐ์ ์ ๋ถ์ฑ๋ฅผ ํด์ํ๊ณ , ๋ฐ์ดํฐ๋ฅผ ์ถ์์ ์ด๊ณ WordPress์ ์ข ์๋์ง ์๋ ๋ฐฉ์์ผ๋ก ๋ ธ์ถํ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํฉ๋๋ค
๋ ์ ๊ทผ ๋ฐฉ์ ๋ชจ๋ ์ฅ๋จ์ ์ด ์์ผ๋ฉฐ, ์ด๋ ์ชฝ์ด ๋ง๊ณ ํ๋ฆฌ๋ค๋ ๊ฒ์ ์์ต๋๋ค. ๋จ์ง ์ด๋ค ๋์์ ๋ค๋ฅธ ๊ฒ๋ณด๋ค ์ฐ์ ์ํ๋ ์ฃผ๊ด์ ์ธ ์ ํ์ผ ๋ฟ์ ๋๋ค.
ํ๋ฌ๊ทธ์ธ Gato GraphQL ์์๋ ํ์์ ์ ๊ทผ ๋ฐฉ์์ ์ ํํ์ฌ, WordPress๋ฅผ ๊ธฐ๋ฐ์ผ๋ก WordPress๋ฅผ ์ํด ๋์ํ๋ฉด์๋ WordPress์ ์ข ์๋์ง ์๋ (์๋ฅผ ๋ค์ด, ์ผ๊ด์ฑ ์๋ ์ด๋ฆ๊ณผ ๊ด๊ณ๋ฅผ ์ ๊ฑฐํ๋) GraphQL ์คํค๋ง๋ฅผ ๋ง๋ค๋ ค ํ์ต๋๋ค.
๊ทธ ๊ฒฐ๊ณผ, GraphQL์ WordPress๋ฅผ ๋์ด๋ฆฝ๋๋ค. ๊ธฐ๋ฐ CMS๋ก์์ WordPress๋ ๊ทธ๋๋ก์ด๊ณ ๋ ๊ฑฐ์ PHP ์ฝ๋๋ ๋ณํ์ง ์์ง๋ง, ๋ฐ์ดํฐ ๊ณ์ธต์ ์ ํต์ด ์๋ ์์์ ๊ธฐ๋ฐํ์ฌ ์๋กญ๊ฒ ๋ง๋ค์ด์ง ์ ์์ต๋๋ค. ๋ฐ์ดํฐ ๊ณ์ธต์ ์ฌ์ถ๊ธฐ์์ ๋ค์ ์ ์๊ธฐ๋ก ๋์๊ฐ๋ ๊ฒ์ ๋๋ค.

๊ทธ ๊ฒฐ๊ณผ๋ WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ์ ํํํ๋ GraphQL ์คํค๋ง์ด๋ฉฐ, ์ค์ฒฉ๋ mutation๋ ์ง์ํฉ๋๋ค.
์ด๋ป๊ฒ ๊ตฌํ๋์๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ
WordPress์๋ ๋ค์๊ณผ ๊ฐ์ ์ํฐํฐ๊ฐ ์์ต๋๋ค.
- posts
- pages
- custom posts
- ๋ฏธ๋์ด ์์
- users
- user roles
- tags
- categories
- comments
- blocks
- meta ์์ฑ
- ๊ธฐํ (options, plugins, themes ๋ฑ)
์ด๋ฌํ ์ํฐํฐ๋ค์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, post, page, ๋ฏธ๋์ด ์์๋ ๋ชจ๋ custom post type์ด๋ฉฐ, tags์ categories๋ ๋ชจ๋ ํ์๋ ธ๋ฏธ์ ๋๋ค.
๋ค์์ WordPress ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ค์ด์ด๊ทธ๋จ์ผ๋ก, ๋ชจ๋ ์ํฐํฐ์ ๋ฐ์ดํฐ๊ฐ ์ด๋ป๊ฒ ์ ์ฅ๋๋์ง๋ฅผ ๋ณด์ฌ์ค๋๋ค.

๋งคํ์ด DB ๋ค์ด์ด๊ทธ๋จ์ ์์ ํ ๋ณต์ ์ธ๊ฐ์?
WordPress ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ GraphQL ์คํค๋ง๋ก ๋งคํํ ๋, ์์ ๋ค์ด์ด๊ทธ๋จ์ 1:1๋ก ๋ฐ๋ฅผ ํ์๊ฐ ์์๊น์?
์๋์, ๊ทธ๋ ์ง ์์ต๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ค์ด์ด๊ทธ๋จ์ ์ค์ ๊ตฌํ์ด์ง๋ง, GraphQL์ ํด๋ผ์ด์ธํธ์์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํ ์ธํฐํ์ด์ค์
๋๋ค. ์ด ๋ ๊ฐ์ง๋ ๊ด๋ จ์ด ์์ง๋ง ์๋ก ๋ฌ๋ผ๋ ๋ฉ๋๋ค. GraphQL์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ๊ฒฝ ์ฐ์ง ์์ต๋๋ค. SQL ๋ช
๋ น์ผ๋ก ์๊ฐํ๊ฑฐ๋, wp_posts๋ wp_users๋ผ๋ ํ
์ด๋ธ์ด ์กด์ฌํ๋ค๋ ๊ฒ์ ์ ํ์๊ฐ ์์ต๋๋ค.
๋ฐ๋ผ์ WordPress์ GraphQL ์คํค๋ง๋ฅผ ๋ง๋ค ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ค์ด์ด๊ทธ๋จ์ ๊ทธ๋๋ก ๋ฐ๋ฅผ ํ์๋ ์์ต๋๋ค. ์ฆ, WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ์ด ๊ฐ์ง ๊ธฐ์ ์ ๋ถ์ฑ์ ์ผ๋ถ๋ฅผ ํด์ํ๋ GraphQL ์คํค๋ง๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ์ GraphQL ์คํค๋ง๋ก ๋งคํํ๊ธฐ
๋งคํ์ ์งํํด ๋ด ์๋ค. ๋จผ์ ์๋ ์ํฐํฐ๋ฅผ ๊ฐ๋ฅํ ํ type์ผ๋ก ๋งคํํฉ๋๋ค. WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ์ ์ํฐํฐ ๋ชฉ๋ก์์ GraphQL ์คํค๋ง๋ฅผ ์ํ ๋ค์ type๋ค์ ์์ฑํฉ๋๋ค.
PostPageMediaUserUserRolePostTagPostCategoryComment
๊ทธ๋ฐ ๋ค์ ๋ชจ๋ type์ ๊ธฐ๋๋๋ ํ๋๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์คํค๋ง๋ฅผ ํํํ๊ธฐ ์ํด SDL, ์ฆ Schema Definition Language๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. (์ด๋ ๋ฌธ์ํ ๋ชฉ์ ์ผ๋ก๋ง ์ฌ์ฉ๋ฉ๋๋ค. ํ๋ฌ๊ทธ์ธ ์์ฒด๋ SDL๋ก ์คํค๋ง๋ฅผ ์ฝ๋ํํ์ง ์์ผ๋ฉฐ, ๋ชจ๋ PHP ์ฝ๋์ ๋๋ค.)
Post์ ํ๋ (๊ทธ ์ธ ๋ค์ ์ค์์) ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
type Post {
id: ID!
title: String
content: String
excerpt: String
publishedAt: Date!
}User์ ํ๋ (๊ทธ ์ธ ๋ค์ ์ค์์) ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
type User {
id: ID!
name: String
email: String!
}๋์ํ๋ ์ฐ๊ฒฐ(connection)๋ ์์ฑํฉ๋๋ค. ์ฐ๊ฒฐ์ด๋ ์ค์นผ๋ผ(์ซ์๋ ๋ฌธ์์ด ๋ฑ)๊ฐ ์๋ ๋ค๋ฅธ ์ํฐํฐ๋ฅผ ๋ฐํํ๋ ํ๋์ ๋๋ค. ์๋ฅผ ๋ค์ด, post์ ์ ์๊ฐ ์๊ณ , user๊ฐ posts๋ฅผ ์์ ํ๋ค๋ ๊ฒ์ ํํํฉ๋๋ค.
type Post {
author: User!
}
type User {
posts: [Post]
}ํ๋์ ์ฐ๊ฒฐ์ ์ธ์๋ฅผ ๋ฐ์ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, Post.date๋ฅผ ํฌ๋งท ๊ฐ๋ฅํ๊ฒ ํ๊ณ , User.posts์์ ํญ๋ชฉ ๊ฒ์๊ณผ ์ ์ ํ์ ํ ์ ์๊ฒ ํฉ๋๋ค.
type Post {
date(format: String): Date!
}
type User {
posts(limit: Int, search: String): [Post]
}WordPress ๋ฐ์ดํฐ ๋ชจ๋ธ์ ๋ชจ๋ ์ํฐํฐ์ ๋ํด ์ด ์์ ์ ๊ณ์ํฉ๋๋ค. ์์ฑ๋๋ฉด Voyager ํด๋ผ์ด์ธํธ(ํ๋ฌ๊ทธ์ธ ๋ฉ๋ด์์ "Interactive Schema"๋ก ์ด์ฉ ๊ฐ๋ฅ)๋ฅผ ์ฌ์ฉํ์ฌ ํ์ธํ ์ ์๋ WordPress์ GraphQL ์คํค๋ง๊ฐ ์์ฑ๋ฉ๋๋ค.

์ด ์คํค๋ง๋ WordPress ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ค์ด์ด๊ทธ๋จ๊ณผ ์ ์ฌํ ๋ถ๋ถ๋ ์์ง๋ง ๋ง์ ์ฐจ์ด์ ๋ ์์ต๋๋ค. ๋ถ์ํด ๋ณด๊ฒ ์ต๋๋ค.
์ํฐํฐ ์๋ ์์ ์ Root ํ๋๋ก ๋งคํ๋ฉ๋๋ค
WordPress ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ค์ด์ด๊ทธ๋จ์ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ ๋ฐฉ์์ ๋ํ๋ด๋ฏ๋ก ใ์์์ ใ์ด ์์ต๋๋ค. ๊ทธ๋ฌ๋ GraphQL์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๊ธฐ ์ํ ์ธํฐํ์ด์ค์ด๋ฏ๋ก, ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ธฐ ์ํ ์ด๊ธฐ ๋จ๊ณ๊ฐ ํ์ํฉ๋๋ค.
์ด ์ด๊ธฐ ๋จ๊ณ๊ฐ Root type์ด๋ฉฐ, ๋ ์ ํํ๊ฒ๋ QueryRoot์ MutationRoot๋ผ๋ ๋ type(๊ฐ๊ฐ queries์ mutations๋ฅผ ์ฒ๋ฆฌ)์
๋๋ค.
์ด ๋ type์๋ get_posts(), get_users(), wp_signon()์ฒ๋ผ ์ํฐํฐ์ ์์กดํ์ง ์๋ ๋ชจ๋ ์์
์ ๋งคํํฉ๋๋ค.
type QueryRoot {
posts: [Post]!
users: [User]!
}
type MutationRoot {
logUserIn(username: String, password: String): User
}ํ๋๊ฐ ๋ํ๋ด๋ ์์
๊ณผ ๋์ผํ ์ด๋ฆ์ด๋ ์๊ทธ๋์ฒ๋ฅผ ๊ฐ์ง ํ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ํ๋ ์ด๋ฆ์ signOn๋ณด๋ค logUserIn์ผ๋ก ํ๋ ๊ฒ์ด ๋ ์ ํฉํ๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค.
๋ชจ๋ mutation์ MutationRoot ์๋์ ๋ก๋๋ค
wp_update_post()์ฒ๋ผ ํน์ ์ํฐํฐ์ ์์กดํ๋ ์์
๋ ์์ต๋๋ค. ์ด๋ ์ด๋ค post์ ์ ์ฉ๋ฉ๋๋ค. ๋์ํ๋ mutation์ GraphQL ์คํค๋ง์ MutationRoot type์ ์ถ๊ฐํด์ผ ํฉ๋๋ค. ์ด๊ฒ์ด GraphQL์ ์ฌ์์
๋๋ค.
์ด ์์ ์ ๋ค์๊ณผ ๊ฐ์ด ๋งคํ๋ฉ๋๋ค.
type MutationRoot {
updatePost(input: {
postID: ID!,
newTitle: String,
newContent: String
}): Post
}์ด ํ๋ฌ๊ทธ์ธ์ ์ค์ฒฉ๋ mutation๋ ์ง์ํฉ๋๋ค (ํ์ค GraphQL ๋์์ด ์๋๋ฏ๋ก ์ตํธ์ธ ๊ธฐ๋ฅ์ผ๋ก ์ ๊ณต). ์ด ๊ฒฝ์ฐ mutation์ MutationRoot๋ฟ๋ง ์๋๋ผ ๋ชจ๋ type ์๋์ ์ถ๊ฐํ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ๋ฉ๋๋ค.
type Post {
update(input: {
newTitle: String,
newContent: String
}): Post!
}custom posts ์ฒ๋ฆฌํ๊ธฐ
GraphQL์๋ type ์์์ด ์์ต๋๋ค. ๋ฐ๋ผ์ CustomPost๋ผ๋ type์ ๊ฐ์ง๊ณ , Post์ Page๊ฐ ์ด๋ฅผ ์์ํ๋ ๊ตฌ์กฐ๋ฅผ ์ทจํ ์ ์์ต๋๋ค.
GraphQL์ ์ด ์ ์ฝ์ ๋ณด์ํ๊ธฐ ์ํด ๋ ๊ฐ์ง ์๋จ์ ์ ๊ณตํฉ๋๋ค. ์ธํฐํ์ด์ค์ union type์ ๋๋ค.
์ฒซ ๋ฒ์งธ๋ก, ์คํค๋ง์ CustomPost ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ณ custom post์์ ๊ธฐ๋๋๋ ๋ชจ๋ ํ๋๋ฅผ ์ ์ธํ๋ฉฐ, Post์ Page๊ฐ ๊ทธ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋๋ก ์ ์ํฉ๋๋ค.
interface CustomPost {
title: String
content: String
excerpt: String
date(format: String): Date!
}
type Post implements CustomPost {
title: String
content: String
excerpt: String
date(format: String): Date!
}
type Page implements CustomPost {
title: String
content: String
excerpt: String
date(format: String): Date!
}๋ ๋ฒ์งธ๋ก, ๋ชจ๋ custom post type์ ๋ฐํํ๋ CustomPostUnion type์ ์คํค๋ง์ ๋ง๋ญ๋๋ค.
union CustomPostUnion = Post | Page๊ทธ๋ฆฌ๊ณ ์ ์ ํ ๊ฒฝ์ฐ์ ์ด type์ ๋ฐํํ๋ ํ๋๋ฅผ ์ ์ํฉ๋๋ค.
type QueryRoot {
customPost(id: ID): CustomPostUnion
customPosts: [CustomPostUnion]!
}
type User {
customPosts: [CustomPostUnion]
}
type Comment {
customPost: CustomPostUnion!
}๋ณด์๋ค์ํผ, GraphQL ์คํค๋ง์์๋ posts๋ฅผ ๋ค๋ฃจ๊ณ ์๋์ง custom posts๋ฅผ ๋ค๋ฃจ๊ณ ์๋์ง ๋ช ์์ ์ผ๋ก ๊ตฌ๋ณํด์ผ ํฉ๋๋ค. ์ด ๋์ ๊ฐ์ง ์๊ธฐ ๋๋ฌธ์ ๋๋ค! ์ด ๋ ๊ฐ์ง๋ฅผ ํผ์ฉํ๋ ๊ฒ์ WordPress์ ๊ธฐ์ ์ ๋ถ์ฑ์ด๋ฉฐ, ์์ ํ ์ ์์ต๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ custom post๋ ํญ์ Post๊ฐ ์๋ CustomPost๋ผ๊ณ ๋ถ๋ฅด๊ณ , custom posts๋ฅผ ๋ค๋ฃจ๋ ํ๋๋ ํญ์ posts๊ฐ ์๋ customPosts๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ, custom post์ ID๋ฅผ ๋ฐ๋ ํ๋ ์ธ์๋ ๋งคํ๋๋ WordPress ํจ์์์์ ์ด๋ฆ์ด postID๋ผ ํ๋๋ผ๋ customPostID๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
์ด๋ ๊ฒ ํจ์ผ๋ก์จ ๊ธฐ๋ํ๋ ๋์์ด ํญ์ ๋ช ํํด์ง๋๋ค.
- ํ๋
User.customPosts๋ posts์ pages๋ฅผ ํฌํจํ ๋ชจ๋ custom post ๋ชฉ๋ก์ ๋ฐํํ ์ ์๋ ๋ฐ๋ฉด,User.posts๋ posts๋ง ๋ฐํํฉ๋๋ค - ํ๋
Root.setFeaturedImageOnCustomPost๋ ๋ชจ๋ custom post์ ๋ํ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค (๊ทธ๋์setFeaturedImageOnPost๋ผ๊ณ ๋ถ๋ฅด์ง ์์ต๋๋ค)
tags (์ categories)๋ฅผ ๋จ์ผ type์ผ๋ก ๋ฌถ์ง ์๊ธฐ
์ PostTag (PostCategory๋ ๋ง์ฐฌ๊ฐ์ง)๋ผ๋ type ์ด๋ฆ์ ์ฌ์ฉํ๋ ๊ฑธ๊น์? ๋จ์ํ Tag๋ผ๊ณ ํ๋ฉด ์ ๋๋ ๊ฑธ๊น์?
์๋ํ๋ฉด, ์ด ์ฟผ๋ฆฌ๋ฅผ ์คํํ ๋ (product๋ฅผ CPT๋ก ๊ฐ์ ), posts์ products์ tags ํ๋ ๊ฒฐ๊ณผ๋ ํญ์ ๋ค๋ฅด๊ณ ๊ฒน์น์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค.
query {
posts {
tags {
id
name
}
}
products {
tags {
id
name
}
}
}posts์ ์ถ๊ฐ๋ tags๋ products์ tags๋ฅผ ๊ฒ์ํด๋ ๋ํ๋์ง ์์ต๋๋ค (๋ฐ๋์ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค). ๋จ, product๊ฐ post_tag ํ์๋
ธ๋ฏธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ PostTag type์ผ๋ก ํํํ ์๋ ์์ต๋๋ค. WordPress์์๋ ์ด๋ฌํ ํญ๋ชฉ๋ค์ ๋์ผํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ
์ด๋ธ์ ๋ค๋ฅธ ํ์ผ๋ก ์ทจ๊ธํ ์ ์์ด ํฐ ๋ฌธ์ ๊ฐ ์๋์ง๋ง, ๊ฐํ ํ์
์์คํ
์ ๊ฐ์ง GraphQL์์๋ ์ค์ํ ๊ตฌ๋ณ์
๋๋ค.
๋ฐ๋ผ์ ์ด๋ฌํ ์ํฐํฐ๋ค์ ๊ฐ์์ type์ผ๋ก ๋ถ๋ฆฌํด ๋๋ ๊ฒ์ด ์ข์ ์ค๊ณ์
๋๋ค. posts์ tags๋ PostTag type์ผ๋ก ๋ฐํํ๊ณ , ์ปค์คํ
ํ๋ฌ๊ทธ์ธ์ด ์์ฒด product CPT๋ฅผ ๊ตฌํํ๋ ๊ฒฝ์ฐ ํด๋น tags์ ProductTag type์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
๋ฏธ๋์ด ์์ดํ ์ ๊ณ ์ ํ ์ ์ฒด์ฑ ๋ถ์ฌํ๊ธฐ
WordPress์ ๋ฏธ๋์ด ์ํฐํฐ๋ ๊ตฌํ์์ ํธ์๋ฅผ ์ํด custom post type์ผ๋ก ์ทจ๊ธ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ GraphQL ์คํค๋ง์์๋ ์ด ๊ธฐ์ ์ ๋ถ์ฑ๋ฅผ ํผํ๊ณ , ๋ฏธ๋์ด ์์๋ฅผ custom posts๊ฐ ์๋ ๊ณ ์ ํ ์ํฐํฐ๋ก ๋ชจ๋ธ๋งํ ์ ์์ต๋๋ค.
์ด์ ๋ฐ๋ผ GraphQL ์คํค๋ง์์๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ์ ์ด ๋ด๋ ค์ง๋๋ค.
customPostsํ๋๋ฅผ ์ฟผ๋ฆฌํด๋ ๋ฏธ๋์ด ์์๋ ๊ฐ์ ธ์ค์ง ์์ต๋๋คMediatype์CustomPost์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ง ์์ผ๋ฉฐ,CustomPostUniontype์ ํฌํจ๋์ง ์์ต๋๋คMediatype์ custom post type์์ ๊ธฐ๋๋๋excerpt,date,status๋ฑ์ ํ๋๋ฅผ ๊ฐ์ง ์์ต๋๋ค. ๋์ , ๋ฏธ๋์ด ์์์์ ๊ธฐ๋๋๋ ํ๋๋ง ๊ฐ์ต๋๋ค.
type Media {
id: ID!
src: String!
width: Int
height: Int
}enum ์๋ณ ๋ฐ ๋งคํ
WordPress์์๋ ํน์ ๊ฐ ์งํฉ์์ ๊ณ ์ ๋ ๊ฐ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, post์ ์ํ๋ "publish", "draft", "pending", "trash" ์ค ํ๋๋ง ๊ฐ๋ฅํฉ๋๋ค.
GraphQL์์๋ ์ด๊ฒ๋ค์ string ๋์ enum์ผ๋ก ์ทจ๊ธํ๊ณ , ๋์ํ๋ enumeration type์ ๋ง๋ค ์ ์์ต๋๋ค. GraphQL ํ์ค์ ๋ฐ๋ฅด๋ฉด enum์ ๋๋ฌธ์๋ก ์์ฑ๋ฉ๋๋ค.
enum CUSTOM_POST_STATUS {
PUBLISH
DRAFT
PENDING
TRASH
}๊ทธ๋ฌ๋ ๊ทธ๋ ๊ฒ ํ๋ฉด WordPress์ ์ง์ ์ํธ ์์ฉํ๊ธฐ ์ํด ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋ฉ๋๋ค. get_posts( [ "post_status" => "PUBLISH" ] )๊ฐ ์๋ํ์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค.
๊ทธ๋์ ํํ์์ผ๋ก, ์ด๋ฌํ enum ๊ฐ๋ค์ ์๋ฌธ์๋ก ์ ์งํฉ๋๋ค.
enum CUSTOM_POST_STATUS {
publish
draft
pending
trash
}์ถ๊ฐ type ๋งคํ
๋ธ๋ก์ WordPress ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ค์ด์ด๊ทธ๋จ์์ ์ง์ ๋ณด์ด์ง ์์ต๋๋ค. wp_posts์ ์ ์ฅ๋์ด ์๊ณ (wp_blocks๋ผ๋ ํ
์ด๋ธ์ ์กด์ฌํ์ง ์์), ๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ ๋
์์ ์ธ ์ํฐํฐ์
๋๋ค.
๋ฐ๋ผ์ ๋ธ๋ก์ ๋งคํํ๊ธฐ ์ํด Block type์ ๋์
ํฉ๋๋ค.
type Post {
blocks: [Block]
}
type Block {
type: String!
attributes: JSONObject
}