Demo Gato GraphQL + Meta Box

Sincronizzare un articolo tra 2 siti, inclusi i metadati Meta Box (e anche Slim SEO)

Sincronizza un articolo e i suoi dati Meta Box (e anche Slim SEO) da un sito WordPress a un altro, con Gato GraphQL per WordPress

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image
Target Image

Possiamo sincronizzare un articolo da un sito WordPress a un altro, inclusi i metadati gestiti tramite Meta Box, o aggiunti da Slim SEO (o da altri plugin).

In questa demo, useremo GraphQL per:

  1. Recuperare un articolo e tutti i suoi metadati dal sito sorgente
  2. Creare un nuovo articolo o aggiornare un articolo esistente sul sito di destinazione, e copiare i dati e i metadati dell'articolo dal sito di origine

Questa query richiede:

  • Gato GraphQL + estensioni PRO sul sito sorgente
  • Il plugin Gato GraphQL gratuito sul sito di destinazione
  • Mutation annidate abilitate sull'endpoint del sito di destinazione

Dobbiamo fornire le seguenti variabili:

  • postType: Il custom post type dell'articolo da sincronizzare tra i siti
  • postSlug: Lo slug dell'articolo da sincronizzare tra i siti
  • downstreamServerGraphQLEndpointURL: L'URL dell'endpoint GraphQL del sito WordPress di destinazione
  • username: Il nome utente della password applicativa per autenticarsi sul sito di destinazione
  • appPassword: La password della password applicativa per autenticarsi sul sito di destinazione
  • update: Se aggiornare un articolo esistente (true) o crearne uno nuovo (false)

Se si aggiorna l'articolo, l'identificatore comune tra il sito a monte e quello a valle è lo slug dell'articolo.

La query GraphQL deve essere eseguita sul sito di origine.

Ecco la query 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
          }
        ]
      }
    }
  )
}

Le variabili sarebbero simili a queste:

{
  "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 }"
}

Iscriviti alla nostra newsletter

Resta aggiornato su tutte le novità di Gato GraphQL.