๐จ๐ปโ๐ป GraphQL, ์ผ์ข ์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก์
GraphQL์ GraphQL ์ธ์ด๋ฅผ ๊ฐ์ถ๊ณ ์์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ผ๊ณ ๋ถ๋ฅด์ง๋ ์์ต๋๋ค. ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก ํ ์ ์๋ ๋ง์ ๊ฒ๋ค์ GraphQL๋ก๋ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
GraphQL์ ๋ณดํต ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด ํด๋ผ์ด์ธํธ์์ ์น์ฌ์ดํธ๋ฅผ ๋ ๋๋งํ๊ฑฐ๋, ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด ๊ฒ์๋ฌผ์ ์์ฑํ๋ ๊ฒฝ์ฐ๊ฐ ๊ทธ๋ ์ต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๊ฒ์ด ์ ๋ถ์ ๋๋ค.
(๋ค๋ฅธ ์ฉ๋๋ ๋จ์ํ ์ด ๋ ๊ฐ์ง ๊ฒฝ์ฐ์ ์กฐํฉ์ ๋๋ค. ์๋ฅผ ๋ค์ด, API ๊ฒ์ดํธ์จ์ด๋ ํด๋ผ์ด์ธํธ์ ๋ ธ์ถ๋์ง ์์ ๋ด๋ถ ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.)
GraphQL์์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๊ฒฝ์ฐ:
query PrintPostTitle($postID: ID!)
{
post(by: { id: $postID }) {
title
}
}...์ด๊ฒ์ PHP์์ ๋ค์๊ณผ ๊ฐ์ด (๊ฑฐ์) ๋๋ฑํฉ๋๋ค:
function printPostTitle(int $postID)
{
$post = getPost($postID);
echo $post->title;
}(์๋์ ๋ชจ๋ ์์๋ ๋น๊ต๋ฅผ ์ํ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก PHP๋ฅผ ์ฌ์ฉํฉ๋๋ค.)
GraphQL์์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ:
query UpdatePost($postID: ID!, $title: String!)
{
updatePost(
by: { id: $postID },
input: { title: $title }
) {
title
}
}...์ด๊ฒ์ PHP์์ ๋ค์๊ณผ ๊ฐ์ด (๊ฑฐ์) ๋๋ฑํฉ๋๋ค:
function updatePost(int $postID, string $title)
{
$post = getPost($postID);
$post->update(['title' => $title]);
}GraphQL์ ๋ณดํต ํด๋ผ์ด์ธํธ(JavaScript, PHP, Java ๋ฑ์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก ์์ฑ๋)์์ ์ ๊ทผํ๋ฉฐ, ํด๋ผ์ด์ธํธ๊ฐ ๋ฐ์ดํฐ๋ก ๋ฌด์์ ํ ์ง์ ๋ํ ๋ก์ง์ ํฌํจํ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ผ๋ก ์ถฉ๋ถํฉ๋๋ค. ๋ฐ๋ผ์ GraphQL์ ๋จ๋ ์ผ๋ก ์ฌ์ฉ๋๋ ๊ฒ์ด ์๋๋ผ ๋ค๋ฅธ ๋ฌด์ธ๊ฐ์ ๋๋ฐ์๋ก ์ฌ์ฉ๋ฉ๋๋ค.
๊ทธ๋ฌ๋ GraphQL์ด ๋จ๋ ์ผ๋ก ์ฌ์ฉ๋ ์ ์๋ค๋ฉด, GraphQL๋ง์ ์ฌ์ฉํ์ฌ ๋ง์ ์๋ก์ด ์ ์ค์ผ์ด์ค๋ฅผ ํด๊ฒฐํ ์ ์์ผ๋ฉฐ, GraphQL์ ์๋ก์ด ํ๊ฒฝ์ ๋ฐฐํฌํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์คํ์์ ์ถ๊ฐ์ ์ธ ์์ ์ ๋ด๋นํ๊ฒ ํ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๊ธฐ ์ํด์๋ GraphQL์ด ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ง์ ๊ธฐ๋ฅ์ ์ง์ํด์ผ ํฉ๋๋ค.
GraphQL์ด ์ง์ํ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๊ธฐ๋ฅ์ ์ ํ์ ์
๋๋ค. ์๋ฅผ ๋ค์ด, ๋๋ ํฐ๋ธ @include(๋๋ @skip)๋ฅผ ์ฌ์ฉํ๊ณ ๋ณ์๋ฅผ ์
๋ ฅ์ผ๋ก ์ ๋ฌํ๋ ๊ฒ์ (์ผ์ข
์) ์กฐ๊ฑด ๋ก์ง์ผ๋ก ๋ณผ ์ ์์ต๋๋ค:
query PrintPostProperties($postID: ID!, $addContent: Boolean!)
{
post(by: { id: $postID }) {
title
content @include(if: $addContent)
}
}์ด ์ฟผ๋ฆฌ๋ PHP์์ ๋ค์๊ณผ ๋๋ฑํฉ๋๋ค:
function printPostProperties(int $postID, bool $addContent)
{
$post = getPost($postID);
echo $post->title;
if ($addContent) {
echo $post->content;
}
}๊ทธ๊ฒ์ด ๊ธฐ๋ณธ์ ์ธ ํ๊ณ์ ๋๋ค. GraphQL์๋ ์ฌ๊ท, ๋์ ๋ณ์(๊ฐ์ด ๋ฐํ์์ ๊ณ์ฐ๋์ด ๋ณ์์ ํ ๋น๋๋ ๊ฒ์ผ๋ก, ๋์ ๋๋ฆฌ์ ์ ๋ ฅ์ผ๋ก ์ ๋ฌ๋๋ ๊ฒ์ด ์๋), ๋ณ์ ํ ๋น(์: ํ๋ ์ถ๋ ฅ์ ๋ณ์์ ํ ๋นํ๊ณ , ์ด๋ฅผ ๋ค๋ฅธ ํ๋์ ์ธ์๋ก ์ ๊ณต) ๋ฑ์ด ๋ถ์กฑํฉ๋๋ค.
GraphQL๋ง์ ์ฌ์ฉํ์ฌ ๋ค์ ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ ์ง ์๊ฐํด ๋ณด์ธ์:
- ์๋น์ค์ ์๋ก์ด ์ฌ์ฉ์๊ฐ ๊ฐ์
ํ ๋๋ง๋ค ํด๋น ์๋น์ค์ ์ํด ํธ์ถ๋๋ ์นํ
์ ๋ง๋์ธ์. ์ฌ์ฉ์๊ฐ ๋ด์ค๋ ํฐ๋ฅผ ๊ตฌ๋
ํ์ ์ ์์ผ๋ฉฐ(์นํ
ํ์ด๋ก๋์
marketing_optinํ๋๋ก ํ์๋จ), ๊ทธ ๊ฒฝ์ฐ ์นํ ์ ์ฌ์ฉ์์ ์ด๋ฉ์ผ(์นํ ํ์ด๋ก๋์emailํ๋)์ Mailchimp ๋ชฉ๋ก์ ๋ฑ๋กํด์ผ ํฉ๋๋ค.
๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํ์๋์? ์ฝ๋์? ์ด๋ ค์ด๊ฐ์? ๋ถ๊ฐ๋ฅํ๊ฐ์?
Gato GraphQL์์๋ ์ด ๋ฌธ์ ๋ฅผ GraphQL๋ง์ผ๋ก ํด๊ฒฐํ๊ณ ์ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ ๋ง์ ๋ฌธ์ ๋ค๋์. ๊ทธ๋์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ํน์ฑ์ ์ง์ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๊น์ด ๊ณ ๋ฏผํด ์์ต๋๋ค.
GraphQL ์๋ฒ์์ ์ง์ํ ํ๋ก๊ทธ๋๋ฐ ๊ธฐ๋ฅ๋ค์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์ด ๊ธ์ ๋ง์ง๋ง์์ ๊ทธ ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์๋์ง ํ์ธํ๊ฒ ์ต๋๋ค.
๊ธฐ๋ฅ์ฑ(Functionality)
GraphQL์ ํ๋๋ ๋ณดํต ๊ฒ์๋ฌผ์ ์ ๋ชฉ, ์ฝํ ์ธ , ๋ฐ์ดํฐ์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค. ํ์ง๋ง ํ๋๋ฅผ "๊ธฐ๋ฅ์ฑ"์ผ๋ก ๊ตฌํํ ์๋ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, PHP์์ ์๊ฐ์ ์ถ๋ ฅํ๋ ๊ฒฝ์ฐ:
function printTime()
{
echo time();
}...์ด๊ฒ์ GraphQL์ ํ๋ _time์ผ๋ก ๊ตฌํํ ์ ์์ต๋๋ค:
{
_time
}ํจ์ time์ ์ด๋ค ํ์
์๋ ์ํ์ง ์์ผ๋ฏ๋ก, ํ๋ _time๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค. ๋ฐ๋ผ์ ์ด๊ฒ์ ๊ธ๋ก๋ฒ ํ๋์ด๋ฉฐ, GraphQL ์คํค๋ง์ ๋ชจ๋ ํ์
์์ ์ ๊ทผํ ์ ์์ต๋๋ค:
{
posts {
_time
}
}๋ค๋ฅธ ๊ธฐ๋ฅ์ฑ ํ๋์ ์์:
_arrayItem_arrayJoin_date_equals_inArray_intAdd_isEmpty_isNull_makeTime_objectProperty_sprintf_strContains_strRegexReplace_strSubstr
ํจ์(Functions)
๋ก์ง์ ๋จ์๋ฅผ ํจ์๋ก ๋ถํ ํ๊ณ , ํ ํจ์๊ฐ ๋ค๋ฅธ ํจ์๋ฅผ ํธ์ถํ๋๋ก ํ ์ ์์ต๋๋ค:
function printPostProperties(int $postID)
{
$post = getPost($postID);
printPostTitle();
printPostContent();
}
function printPostTitle(Post $post)
{
echo $post->title;
}
function printPostContent(Post $post)
{
echo $post->content;
}GraphQL์์๋ ๋ง์ฐฌ๊ฐ์ง๋ก, ๋ฌธ์์ query(๋๋ mutation) ์์
์ ์ฌ๋ฌ query ์์
์ผ๋ก ๋ถํ ํ๊ณ , ํ ์์
์ด ๋ค๋ฅธ ์์
์ "์์กด"ํ๋๋ก ํ์ฌ ๋จผ์ ์คํ๋๊ฒ ํ ์ ์์ต๋๋ค:
query PrintPostTitle($postID: ID!)
{
postWithTitle: post(by: { id: $postID }) {
title
}
}
query PrintPostContent($postID: ID!)
{
postWithContent: post(by: { id: $postID }) {
content
}
}
query PrintPostProperties
@depends(on: [
"PrintPostTitle",
"PrintPostContent"
])
{
# ...
}์ด ์ฟผ๋ฆฌ์์ ์๋ํฌ์ธํธ์ ?operationName=PrintPostProperties๋ฅผ ์ ๋ฌํ์ฌ GraphQL ์ฟผ๋ฆฌ๋ฅผ ์คํํ๋ฉด, ๋จผ์ PrintPostTitle๊ณผ PrintPostContent ์ฟผ๋ฆฌ๊ฐ ์คํ๋๊ณ , ๊ทธ ํ์ PrintPostProperties๊ฐ ์คํ๋ฉ๋๋ค.
์ด๊ฒ์ Multiple Query Execution์ ํตํด ๊ฐ๋ฅํฉ๋๋ค.
๋์ ๋ณ์(Dynamic Variables)
๊ฐ์ ๋ฐํ์์ ๊ณ์ฐํ์ฌ ๋ณ์์ ํ ๋นํ ์ ์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ๊ทธ ๊ฐ์ ๋ฐ๋ผ ์ด๋ค ๊ธฐ๋ฅ์ ์กฐ๊ฑด๋ถ๋ก ์คํํ ์ง ๊ฒฐ์ ํ ์ ์์ต๋๋ค:
function printPostProperties(int $postID)
{
$post = getPost($postID);
echo $post->title;
$addContent = isUserLoggedIn();
if ($addContent) {
echo $post->content;
}
}GraphQL์์๋ ํ ์์ ์์ ๋์ ๋ณ์์ ๊ฐ์ "๋ด๋ณด๋ด๊ณ ", ๋ค๋ฅธ ์์ ์์ ๊ทธ ๊ฐ์ ์ฝ์ ์ ์์ต๋๋ค:
query ExportAddContent
{
addContent: isUserLoggedIn
@export(as: "addContent")
}
query PrintPostProperties($postID: ID!)
@depends(on: "ExportAddContent")
{
post(by: { id: $postID }) {
title
content @include(if: $addContent)
}
}๋ฐํ์์ ๊ณ์ฐ๋ ๊ฐ์ ๋ณด์ ํ๋ ๋ณ์ $addContent๋ PrintPostProperties ์์
์์ ์ฝํ์ง๋ง, ํด๋น ์์
์์ ์ ์ธ๋์ง ์๋๋ค๋ ์ ์ ์ฃผ๋ชฉํ์ธ์. ์ด๊ฒ์ด ๋์ ๋ณ์์ด๊ธฐ ๋๋ฌธ์
๋๋ค.
ํจ์์ ์กฐ๊ฑด๋ถ ์คํ
์์ ์์์ ๋์์ผ๋ก, ๋ก์ง์ ํจ์๋ก ๊ทธ๋ฃนํํ ๋ค์ ๋์ ๋ณ์์ ๊ฐ์ ๋ฐ๋ผ ํจ์๋ฅผ ์กฐ๊ฑด๋ถ๋ก ์คํํ ์ง ๊ฒฐ์ ํ ์ ์์ต๋๋ค:
function printPostProperties(int $postID)
{
$post = getPost($postID);
printPostTitle();
$addContent = isUserLoggedIn();
if ($addContent) {
printPostContent();
}
}
function printPostTitle(Post $post)
{
echo $post->title;
}
function printPostContent(Post $post)
{
echo $post->content;
}GraphQL์์๋ ์์
์ @include ๋๋ ํฐ๋ธ๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค:
query ExportAddContent
{
addContent: isUserLoggedIn
@export(as: "addContent")
}
query PrintPostTitle($postID: ID!)
{
postWithTitle: post(by: { id: $postID }) {
title
}
}
query PrintPostContent($postID: ID!)
@depends(on: "ExportAddContent")
@include(if: $addContent)
{
postWithContent: post(by: { id: $postID }) {
content
}
}
query PrintPostProperties
@depends(on: [
"PrintPostTitle",
"PrintPostContent"
])
{
# ...
}์ด์ $addContent๊ฐ true์ธ ๊ฒฝ์ฐ์๋ง PrintPostContent ์์
์ด ์คํ๋ฉ๋๋ค.
๋ณ์ ํ ๋น ๋ฐ ์ฌ์ ๋ ฅ
์์ ์์๋ฅผ ์กฐ๊ธ ์์ ํด ๋ณด๊ฒ ์ต๋๋ค. ์์ ์์์์๋ ์กฐ๊ฑด "addContent"๊ฐ ์ฌ์ฉ์์ ๋ก๊ทธ์ธ ์ฌ๋ถ์ ์ฐ๊ฒฐ๋์ด ์์์ต๋๋ค.
์ด ๋ค๋ฅธ ์์์์๋, "addContent"๋ ์ค๋์ด ์ฃผ๋ง์ผ ๋ true๊ฐ ๋ฉ๋๋ค. ์ด๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํ ๋ก์ง์ด ํ์ํฉ๋๋ค:
- ์ค๋ ๋ ์ง ๊ฐ์ ธ์ค๊ธฐ
- ๋ ์ง๋ฅผ ์์ผ ์ด๋ฆ(์๋ฌธ์)์ผ๋ก ํ์ํํ๊ธฐ
"saturday"๋๋"sunday"์ธ์ง ํ์ธํ๊ธฐ
PHP์ ๊ฒฝ์ฐ:
function addContent()
{
$today = time();
$dayName = date('l', $today);
$lcDayName = strtolower($dayName);
$isWeekend = in_array(
$lcDayName,
['saturday', 'sunday']
);
return $isWeekend;
}
function printPostProperties(int $postID)
{
$post = getPost($postID);
echo $post->title;
$addContent = addContent();
if ($addContent) {
echo $post->content;
}
}GraphQL์ ๊ฒฝ์ฐ:
query ExportAddContent
{
today: _time
dayName: _date(format: "l", timestamp: $__today)
lcDayName: _strLowerCase(text: $__dayName)
isWeekend: _inArray(
value: $__lcDayName
array: ["saturday", "sunday"],
)
@export(as: "addContent")
}
query PrintPostProperties($postID: ID!)
@depends(on: "ExportAddContent")
{
post(by: { id: $postID }) {
title
content @include(if: $addContent)
}
}ExportAddContent ์์
์์ ์ฟผ๋ฆฌ๋ ๊ฐ ํ๋์ ๊ฐ์ ๋์ ๋ณ์ $__fieldName์ผ๋ก ์๋ ํ๋์์ ์ฆ์ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค. ์ด๋ฅผ ํตํด ํ๋์ ์ถ๋ ฅ์ ๊ฐ์ ์์
๋ด์์ ์ฆ์ ๋ค๋ฅธ ํ๋์ ์
๋ ฅ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๊ฒ์ Field to Input์ ํตํด ๊ฐ๋ฅํฉ๋๋ค.
๊ฐ์ ๋์ ์์
์ด PHP ์์์์๋ ๋ก๊ทธ์ธํ ์ฌ์ฉ์๊ฐ ๊ด๋ฆฌ์์ธ ๊ฒฝ์ฐ ๋ณ์์ ๊ฐ์ ์์ ํ์ฌ, ๊ทธ ๊ฒฝ์ฐ ๊ฒ์๋ฌผ ์ฝํ ์ธ ์ ๊ฒ์๋ฌผ ํธ์ง ๋งํฌ๋ฅผ ์ถ๊ฐํฉ๋๋ค:
function isAdminUser()
{
$user = getCurrentUser();
return in_array("administrator", $user->roles);
}
function printPostContent(int $postID)
{
$post = getPost($postID);
$postContent = $post->content;
$isAdminUser = isAdminUser();
if ($isAdminUser) {
$postContent = sprintf(
'%s<p><a href="%s">%s</a></p>',
$postContent,
$post->edit_url,
'(Admin only) Edit post'
)
}
echo $postContent;
}GraphQL์์๋ ํน์ ํ๋์ ๋ํด ๋ค๋ฅธ ๊ฐ์ ์์ฑํ๋ ์์ ๋๋ ๋ค๋ฅธ ์์ ์ ์กฐ๊ฑด๋ถ๋ก ์คํํ ์ ์์ต๋๋ค:
query InitializeDynamicVariables
{
isAdminUser: _echo(value: false)
@export(as: "isAdminUser")
}
query ExportConditionalVariables
@depends(on: "InitializeDynamicVariables")
{
me {
roleNames
isAdminUser: _inArray(
value: "administrator",
array: $__roleNames
)
@export(as: "isAdminUser")
}
}
query RetrieveContentForAdminUser($postId: ID!)
@depends(on: "ExportConditionalVariables")
@include(if: $isAdminUser)
{
post(by: { id : $postId }) {
originalContent: content
wpAdminEditURL
content: _sprintf(
string: "%s<p><a href=\"%s\">%s</a></p>",
values: [
$__originalContent,
$__wpAdminEditURL,
"(Admin only) Edit post"
]
)
}
}
query RetrieveContentForNonAdminUser($postId: ID!)
@depends(on: "ExportConditionalVariables")
@skip(if: $isAdminUser)
{
post(by: { id : $postId }) {
content
}
}
query ExecuteAll
@depends(on: [
"RetrieveContentForAdminUser",
"RetrieveContentForNonAdminUser"
])
{
# ...
}๊ฐ์ ๋์ ๋ณ์๋ฅผ ์
๋ ฅ์ผ๋ก @include์ @skip ๋๋ ํฐ๋ธ๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ, RetrieveContentForAdminUser์ RetrieveContentForNonAdminUser ์์
์ ์ํธ ๋ฐฐํ์ ์ด ๋ฉ๋๋ค.
๋ฐฐ์ด ๋ฐ๋ณต ์ฒ๋ฆฌ
๋ฐฐ์ด์ ํญ๋ชฉ์ ๋ฐ๋ณต ์ฒ๋ฆฌํ์ฌ ํด๋น ๊ฐ๋ค์ ๋๋ฌธ์๋ก ๋ณํํ๊ณ ์ถ๋ค๊ณ ๊ฐ์ ํด ๋ด ์๋ค:
function printUserRolesAsUppercase(int $userID)
{
$user = getUser($userID);
foreach ($user->roles as $role) {
echo strtoupper($role);
}
}GraphQL์์๋ ๋๋ ํฐ๋ธ @underEachArrayItem์ผ๋ก ๋ฐฐ์ด ํญ๋ชฉ์ ๋ฐ๋ณต ์ฒ๋ฆฌํ๊ณ , ๊ฐ ๊ฐ์ ์ฒด์ธ์ ๋ค์ ๋๋ ํฐ๋ธ(์ด ๊ฒฝ์ฐ @strUpperCase)์ ์ ๋ฌํ ์ ์์ต๋๋ค:
query PrintUserRolesAsUppercase($userID: ID!)
{
user(by: { id: $userID }) {
roles
@underEachArrayItem
@strUpperCase
}
}์ด๊ฒ์ ์ปดํฌ์ ๋ธ ๋๋ ํฐ๋ธ๋ฅผ ํตํด ๊ฐ๋ฅํฉ๋๋ค.
๋๋ CRUD ์์
CRUD๋ Create(์์ฑ), Read(์ฝ๊ธฐ), Update(์์ ), Delete(์ญ์ )์ ์ฝ์๋ก, ๋ฆฌ์์ค(๊ฒ์๋ฌผ, ์ฌ์ฉ์ ๋ฑ)์ ์ ์ฉํ๋ ์์ ์ ๋๋ค.
PHP์์ ๋๋ ์ฝ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
function getPostTitles()
{
$posts = getPosts();
foreach ($posts as $post) {
echo $post->title;
}
}์ด ์ ์ค์ผ์ด์ค๋ GraphQL์์ ์์ฐ์ค๋ฝ๊ฒ ํด๊ฒฐ๋ฉ๋๋ค:
query GetPostTitles
{
posts {
title
}
}PHP์์ ๋๋ ์์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
function updatePostTitlesAsUppercase()
{
$posts = getPosts();
foreach ($posts as $post) {
$post->update(['title' => strtoupper($post->title)]);
}
}GraphQL์์ ๋๋ ์์ ์ ๋ณดํต ๋ชจ๋ ๊ฒ์๋ฌผ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ ์ ์ฉ mutation updatePosts๋ฅผ ์์ฑํ์ฌ ์ง์ํฉ๋๋ค.
์ ๋ ์ด ์ ๊ทผ ๋ฐฉ์์ด ๋ง์์ ๋ค์ง ์์ต๋๋ค. ์คํค๋ง์ mutation ์๋ฅผ ์ฌ์ค์ ๋ ๋ฐฐ๋ก ๋๋ฆฌ๊ณ (ํ๋์ ๋ฆฌ์์ค๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ, ์ฌ๋ฌ ๋ฆฌ์์ค๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ), ๋ ๊ฐ์ง ๋ชจ๋์ ๋ก์ง์ ์ ์งํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋๋ค:
updatePost+updatePostscreatePost+createPosts- ๋ฑ
์ ์๊ฐ์๋ ๋ ์ฐ์ํ ์ ๊ทผ ๋ฐฉ์์ ์ค์ฒฉ mutation์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ์ฌ๊ธฐ์ mutation Post.update๊ฐ ์ฟผ๋ฆฌ๋ ๊ฐ ๋ฆฌ์์ค์ ์ ์ฉ๋ฉ๋๋ค:
mutation UpdatePostTitlesAsUppercase
{
posts {
title
ucTitle: _strUpperCase(text: $__title)
update(
input: { title: $__ucTitle }
) {
status
post {
title
}
}
}
}๊ฐ์ ์ ๊ทผ ๋ฐฉ์์ด ๋ฆฌ์์ค ์ญ์ ์๋ ์๋ํฉ๋๋ค:
function deletePosts()
{
$posts = getPosts();
foreach ($posts as $post) {
$post->delete();
}
}GraphQL์ ๊ฒฝ์ฐ:
mutation DeletePosts
{
posts {
delete {
status
}
}
}์์ฑ์ ๊ฒฝ์ฐ, ๋ฆฌ์์ค๊ฐ ์์ง ์กด์ฌํ์ง ์์ผ๋ฏ๋ก ์ ๋ฌํ์ง ์์ต๋๋ค. ๋์ , ์์ฑํ ๋ชจ๋ ๋ฆฌ์์ค์ ๋ฐ์ดํฐ ์ ๋ ฅ์ด ๋ด๊ธด ๋ฐฐ์ด์ ์ ๊ณตํฉ๋๋ค:
function createPosts()
{
$postDataItems = [
[
'title' => 'First title',
'content' => 'First content',
],
[
'title' => 'Second title',
'content' => 'Second content',
],
];
foreach ($postDataItems as $postDataItem) {
$post = new Post($postDataItem['title'], $postDataItem['content']);
$post->save();
}
}๋จ์ผ createPost mutation์ ์ฌ์ฉํ์ฌ GraphQL์์ ๋๋์ผ๋ก ๊ฒ์๋ฌผ์ ์์ฑํ๋ ๊ฒ์ ๋ค์ ๋ณต์กํ์ง๋ง ์ถฉ๋ถํ ๊ฐ๋ฅํฉ๋๋ค.
์์ด๋์ด๋ ๋ฐ์ดํฐ ์
๋ ฅ ๋ฐฐ์ด์ ๋ฐ๋ณต ์ฒ๋ฆฌํ๊ณ , ๊ฐ๊ฐ์ ๋์ ๋ณ์ $input์ ํ ๋นํ ๋ค์, ๊ทธ ์
๋ ฅ์ ์ ๋ฌํ์ฌ createPost mutation์ ์คํํ๋ ๊ฒ์
๋๋ค. ๋ง์ง๋ง์ผ๋ก ๋์ ๋ณ์ $createdPostIDs์์ ์์ฑ๋ ๊ฒ์๋ฌผ์ ๊ฒฐ๊ณผ ID๋ฅผ ๊ฐ์ ธ์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค:
mutation CreatePosts
@depends(on: "GetPostsAndExportData")
{
createdPostIDs: _echo(value: [
{
title: "First title",
content: "First content"
},
{
title: "Second title",
content: "Second content"
},
])
@underEachArrayItem(
passValueOnwardsAs: "input"
)
@applyField(
name: "createPost"
arguments: {
input: $input
},
setResultInResponse: true
)
@export(as: "createdPostIDs")
}
query RetrieveCreatedPosts
@depends(on: "CreatePosts")
{
createdPosts: posts(
filter: {
ids: $createdPostIDs,
}
) {
title
content
}
}HTTP ์์ฒญ ์ ์ก(๋ฐ ๊ธฐํ ํจ์)
์น ์๋ฒ์ HTTP ์์ฒญ์ ์ ์กํ๋ ๊ฒ์ PHP์์ file_get_contents๋ curl_exec ๊ฐ์ ์ ์ฉ ํจ์๋ก ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
file_get_contents ์ฌ์ฉ:
$xml = file_get_contents("http://www.example.com/file.xml");GraphQL์์๋ HTTP ์์ฒญ ์คํ ๋ก์ง์ _sendHTTPRequest์ ๊ฐ์ ๊ธฐ๋ฅ์ฑ ํ๋๋ก ์ฒ๋ฆฌํ ์ ์์ต๋๋ค:
query {
_sendHTTPRequest(input: {
url: "http://www.example.com/file.xml",
method: GET
}) {
xml: body
}
}๊ฐ์ ๊ฐ๋ ์ด ๋ชจ๋ ๊ธฐ๋ฅ์ ์ ์ฉ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด, PHP์์ ์์ ๊ฐ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๊ทผํฉ๋๋ค:
$mailchimpUsername = constant('MAILCHIMP_API_CREDENTIALS_USERNAME');GraphQL์์ ํด๋นํ๋ ๊ธฐ๋ฅ์ฑ ํ๋๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค:
{
mailchimpUsername: _env(name: "MAILCHIMP_API_CREDENTIALS_USERNAME")
}GraphQL๋ง์ ์ฌ์ฉํ์ฌ ๊ณผ์ ํด๊ฒฐํ๊ธฐ
์ง๊ธ๊น์ง ์ดํด๋ณธ ๋ชจ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ, ์์ ์ ์ํ ๋ฌธ์ ๋ฅผ GraphQL๋ง์ผ๋ก ํด๊ฒฐํ ์ ์๊ฒ ๋์์ต๋๋ค:
- ์๋น์ค์ ์๋ก์ด ์ฌ์ฉ์๊ฐ ๊ฐ์
ํ ๋๋ง๋ค ํด๋น ์๋น์ค์ ์ํด ํธ์ถ๋๋ ์นํ
์ ๋ง๋์ธ์. ์ฌ์ฉ์๊ฐ ๋ด์ค๋ ํฐ๋ฅผ ๊ตฌ๋
ํ์ ์ ์์ผ๋ฉฐ(์นํ
ํ์ด๋ก๋์
marketing_optinํ๋๋ก ํ์๋จ), ๊ทธ ๊ฒฝ์ฐ ์นํ ์ ์ฌ์ฉ์์ ์ด๋ฉ์ผ(์นํ ํ์ด๋ก๋์emailํ๋)์ Mailchimp ๋ชฉ๋ก์ ๋ฑ๋กํด์ผ ํฉ๋๋ค.
ํด๊ฒฐ์ฑ ์ GraphQL ํผ์์คํฐ๋ ์ฟผ๋ฆฌ๋ฅผ ์นํ ์ผ๋ก ์ฌ์ฉํ๊ณ , ๋ค์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๋ ๊ฒ์ ๋๋ค:
query HasSubscribedToNewsletter {
hasSubscriberOptIn: _httpRequestHasParam(name: "marketing_optin")
subscriberOptIn: _httpRequestStringParam(name: "marketing_optin")
isNotSubscriberOptInNAValue: _notEquals(value1: $__subscriberOptIn, value2: "NA")
subscribedToNewsletter: _and(values: [$__hasSubscriberOptIn, $__isNotSubscriberOptInNAValue])
@export(as: "subscribedToNewsletter")
}
query MaybeCreateContactOnMailchimp
@depends(on: "HasSubscribedToNewsletter")
@include(if: $subscribedToNewsletter)
{
subscriberEmail: _httpRequestStringParam(name: "email")
mailchimpUsername: _env(name: "MAILCHIMP_API_CREDENTIALS_USERNAME")
mailchimpPassword: _env(name: "MAILCHIMP_API_CREDENTIALS_PASSWORD")
mailchimpListMembersJSONObject: _sendJSONObjectItemHTTPRequest(input: {
url: "https://us7.api.mailchimp.com/3.0/lists/{listCode}/members",
method: POST,
options: {
auth: {
username: $__mailchimpUsername,
password: $__mailchimpPassword
},
json: {
email_address: $__subscriberEmail,
status: "subscribed"
}
}
})
}์ด ํด๊ฒฐ์ฑ
์์, Mailchimp API์ HTTP ์์ฒญ์ ์คํํ๋ MaybeCreateContactOnMailchimp ์์
์ marketing_optin ํ๋์ ๊ฐ์ ๋ฐ๋ผ ์กฐ๊ฑด๋ถ๋ก ์คํ๋ฉ๋๋ค.
(์ด ์ฟผ๋ฆฌ์ ๋์ ๋ฐฉ์์ ๋ธ๋ก๊ทธ ๊ฒ์๋ฌผ ๐จ๐ปโ๐ซ GraphQL query to automatically send the newsletter subscribers from InstaWP to Mailchimp์์ ํ์ธํ์ธ์.)
GraphQL์ ์๊ฐํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ๊ฐ๋ ฅํฉ๋๋ค!
GraphQL์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ณ๊ฒฝํ๋ ๊ฒ ๊ทธ ์ด์์ ์ฌ์ฉ๋ ์ ์์ต๋๋ค... ๋ฐ์ดํฐ ์ ์, ์ถ๋ ฅ์ ๋์ ์์ , ๋ค์ํ ์ปจํ ์คํธ์ ๋ง๋ ์ฝํ ์ธ ๋ง์ถคํ, ๋จ ๋ช ์ค์ ์ฝ๋๋ก API ๊ฒ์ดํธ์จ์ด ์์ฑ ๋ฑ ๋ค์ํ ์ฉ๋๊ฐ ์์ต๋๋ค.
ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๊ธฐ๋ฅ์ ์ง์ํจ์ผ๋ก์จ, ์์ ๊ณผ์ ๋ฅผ GraphQL๋ง์ผ๋ก ํด๊ฒฐํ๊ณ ํจ๊ป ๋ฐฐํฌํ ํด๋ผ์ด์ธํธ๋ฅผ ๋ถํ์ํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์คํ์ ๋จ์ํํ ์ ์์ต๋๋ค: ์์ง์ด๋ ๋ถํ์ด ์ ์ด์ง๊ณ , ๋ณต์ก์ฑ์ด ์ค์ด๋ค๋ฉฐ, ๋๋ฒ๊น ํด์ผ ํ ์ฝ๋๊ฐ ์ค์ด๋ค๊ณ , ๋ค๋ค์ผ ํ ๊ธฐ์ ์ด ์ ์ด์ง๋๋ค.
GraphQL์ ์ ๋ง ๋๋จํฉ๋๋ค ๐ค