Gato GraphQL + Meta Box demo

Synchronizing a post across 2 sites, including Meta Box (and also Slim SEO) metadata

Synchronize a post and its Meta Box data (and also Slim SEO) from one WordPress site to another, using Gato GraphQL for WordPress

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image
Target Image

We can synchronize a post from one WordPress site to another, including metadata managed via Meta Box, or added by Slim SEO (or by other plugins).

In this demo, we'll use GraphQL to:

  1. Fetch a post and all its metadata from the source site
  2. Either create a new post or update an existing post on the target site, and copy the post data and metadata from the origin site

This query requires:

  • Gato GraphQL + PRO extensions on the source site
  • Free Gato GraphQL plugin on the target site
  • Nested Mutations enabled on the target site's endpoint

We must provide the following variables:

  • postType: The custom post type of the post to synchronize between sites
  • postSlug: The slug of the post to synchronize between sites
  • downstreamServerGraphQLEndpointURL: The GraphQL endpoint URL of the target WordPress site
  • username: Application password's username to authenticate with on the target site
  • appPassword: Application password's password to authenticate with on the target site
  • update: Whether to update an existing post (true) or create a new one (false)

If updating the post, the common identifier between the upstream and downstream sites is the post slug.

The GraphQL query must be executed on the origin site.

Here is the 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
          }
        ]
      }
    }
  )
}

The variables would look like this:

{
  "postType": "post",
  "postSlug": "hello-world",
  "downstreamServerGraphQLEndpointURL": "https://target-site.com/graphql",
  "update": false,
  "username": "admin",
  "appPassword": "cNEp BVPy QVxF eVqH lggt BTb4"
}

Subscribe to our newsletter

Stay in the loop on all new things concerning Gato GraphQL.