Queryライブラリサイト間での投稿の同期
サイト間での投稿の同期
このクエリは、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 }"
}