쿼리 라이브러리사이트 간 게시물 동기화
사이트 간 게시물 동기화
이 쿼리는 2개 사이트 간 게시물 동기화(Meta Box 및 Slim SEO 메타데이터 포함) 데모에서 시연됩니다.
이 쿼리는 메타데이터를 포함하여 하나의 WordPress 사이트에서 다른 사이트로 게시물을 동기화합니다.
다음 작업을 수행합니다:
- 소스 사이트에서 게시물과 모든 메타데이터를 가져옵니다
- 대상 사이트에 새 게시물을 생성하거나 기존 게시물을 업데이트하며, 원본 사이트의 게시물 데이터와 메타데이터를 복사합니다
이 쿼리에는 다음이 필요합니다:
- 소스 사이트에 Gato GraphQL + PRO 확장 기능
- 대상 사이트에 무료 Gato GraphQL 플러그인
- 대상 사이트의 엔드포인트에서 중첩 Mutation 활성화
필수 변수는 다음과 같습니다:
postType: 사이트 간에 동기화할 커스텀 게시물 유형postSlug: 사이트 간에 동기화할 게시물의 슬러그downstreamServerGraphQLEndpointURL: 대상 WordPress 사이트의 GraphQL 엔드포인트 URLusername: 대상 사이트에서 인증할 애플리케이션 비밀번호의 사용자 이름appPassword: 대상 사이트에서 인증할 애플리케이션 비밀번호의 패스워드update: 기존 게시물을 업데이트(true)하거나 새 게시물을 생성(false)할지 여부
게시물을 업데이트하는 경우, 업스트림 사이트와 다운스트림 사이트 간의 공통 식별자는 게시물 슬러그입니다.
GraphQL 쿼리는 원본 사이트에서 실행되어야 합니다.
query CheckHasCustomPost($postSlug: String!, $postType: String! = post)
{
customPost(by: { slug: $postSlug }, status: any, customPostTypes: [$postType])
@fail(
message: "There is no post in the upstream site with the provided slug"
data: {
slug: $postSlug
}
)
{
rawTitle
@export(as: "postTitle")
rawContent
@export(as: "postContent")
rawExcerpt
@export(as: "postExcerpt")
metaKeys(filter: { exclude: [
"_thumbnail_id",
"_edit_last",
] })
meta(keys: $__metaKeys)
@export(as: "postMeta")
}
isMissingPostInUpstream: _isNull(value: $__customPost)
@export(as: "isMissingPostInUpstream")
}
query ExportCreateCustomPostOnTargetSiteGraphQLQuery(
$update: Boolean! = false
)
@depends(on: "CheckHasCustomPost")
@skip(if: $isMissingPostInUpstream)
@skip(if: $update)
{
query: _echo(value: """
mutation CreateCustomPost(
$postType: String! = post
$postSlug: String!
$postTitle: String!
$postExcerpt: String!
$postContent: String!
$postMeta: NullableListValueJSONObject!
) {
createCustomPost(input: {
customPostType: $postType
title: $postTitle,
excerpt: $postExcerpt,
slug: $postSlug,
contentAs: { html: $postContent },
status: draft,
meta: $postMeta,
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
__typename
...on CustomPost {
customPostType
title
excerpt
slug
content
status
}
}
}
}
"""
)
@export(as: "query")
@remove
}
query ExportUpdateCustomPostOnTargetSiteGraphQLQuery(
$update: Boolean! = false
)
@depends(on: "CheckHasCustomPost")
@skip(if: $isMissingPostInUpstream)
@include(if: $update)
{
query: _echo(value: """
mutation UpdateCustomPost(
$postType: String! = post
$postSlug: String!
$postTitle: String!
$postContent: String!
$postExcerpt: String!
$postMeta: NullableListValueJSONObject!
) {
customPost(by: { slug: $postSlug }, status: any, customPostTypes: [$postType]) {
update(input: {
title: $postTitle,
excerpt: $postExcerpt,
contentAs: { html: $postContent },
meta: $postMeta,
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
__typename
...on CustomPost {
customPostType
title
excerpt
slug
content
status
}
}
}
}
}
"""
)
@export(as: "query")
@remove
}
query CreateOrUpdateCustomPostOnTargetSite(
$downstreamServerGraphQLEndpointURL: String!
$postSlug: String!
$username: String!
$appPassword: String!
$postType: String! = post
)
@depends(on: [
"ExportCreateCustomPostOnTargetSiteGraphQLQuery",
"ExportUpdateCustomPostOnTargetSiteGraphQLQuery",
])
@skip(if: $isMissingPostInUpstream)
{
loginCredentials: _sprintf(
string: "%s:%s",
values: [$username, $appPassword]
)
@remove
base64EncodedLoginCredentials: _strBase64Encode(
string: $__loginCredentials
)
@remove
loginCredentialsHeaderValue: _sprintf(
string: "Basic %s",
values: [$__base64EncodedLoginCredentials]
)
@remove
_sendGraphQLHTTPRequest(
input: {
endpoint: $downstreamServerGraphQLEndpointURL,
query: $query,
variables: [
{
name: "postSlug",
value: $postSlug
},
{
name: "postTitle",
value: $postTitle
},
{
name: "postContent",
value: $postContent
},
{
name: "postExcerpt",
value: $postExcerpt
},
{
name: "postMeta",
value: $postMeta
},
{
name: "postType",
value: $postType
}
],
options: {
headers: [
{
name: "Authorization",
value: $__loginCredentialsHeaderValue
}
]
}
}
)
}변수는 다음과 같습니다:
{
"postType": "post",
"postSlug": "hello-world",
"downstreamServerGraphQLEndpointURL": "https://target-site.com/graphql",
"update": false,
"username": "admin",
"appPassword": "{ application password, eg: cNEp BVPy QVxF eVqH lggt BTb4 }"
}