Gato GraphQL + Meta Box demo
Een post synchroniseren tussen 2 sites, inclusief Meta Box- (en Slim SEO-)metagegevens
Synchroniseer een post en zijn Meta Box-gegevens (en Slim SEO) van de ene WordPress-site naar een andere, met Gato GraphQL voor WordPress
Leonardo Losoviz -


Je kunt een post van de ene WordPress-site naar de andere synchroniseren, inclusief metagegevens die worden beheerd via Meta Box of toegevoegd door Slim SEO (of door andere plugins).
In deze demo gebruiken we GraphQL om:
- Een post en al zijn metagegevens op te halen van de bronsite
- Een nieuwe post aan te maken of een bestaande post bij te werken op de doelsite, en de postgegevens en metagegevens van de bronsite te kopiëren
Deze query vereist:
- Gato GraphQL + PRO-extensies op de bronsite
- De gratis Gato GraphQL-plugin op de doelsite
- Geneste mutaties ingeschakeld op het eindpunt van de doelsite
Je moet de volgende variabelen opgeven:
postType: Het aangepaste posttype van de te synchroniseren post tussen sitespostSlug: De slug van de te synchroniseren post tussen sitesdownstreamServerGraphQLEndpointURL: De GraphQL-eindpunt-URL van de doel-WordPress-siteusername: De gebruikersnaam van het applicatiewachtwoord voor authenticatie op de doelsiteappPassword: Het wachtwoord van het applicatiewachtwoord voor authenticatie op de doelsiteupdate: Of een bestaande post bijgewerkt moet worden (true) of een nieuwe aangemaakt (false)
Als je de post bijwerkt, is de gemeenschappelijke identificator tussen de upstream- en downstreamsites de postslug.
De GraphQL-query moet worden uitgevoerd op de bronsite.
Dit is de GraphQL-query:
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
}
]
}
}
)
}De variabelen zouden er als volgt uitzien:
{
"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 }"
}