Gato GraphQL + Meta Box 데모

2개의 사이트 간에 게시물 동기화하기(Meta Box 및 Slim SEO 메타데이터 포함)

Gato GraphQL for WordPress를 사용하여 Meta Box 데이터 및 Slim SEO 데이터를 포함한 게시물을 하나의 WordPress 사이트에서 다른 사이트로 동기화합니다

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image
Target Image

Meta Box로 관리되는 메타데이터나 Slim SEO(또는 다른 플러그인)에 의해 추가된 메타데이터를 포함한 게시물을 하나의 WordPress 사이트에서 다른 사이트로 동기화할 수 있습니다.

이 데모에서는 GraphQL을 사용하여 다음을 수행합니다.

  1. 소스 사이트에서 게시물과 모든 메타데이터를 가져오기
  2. 대상 사이트에 새 게시물을 생성하거나 기존 게시물을 업데이트하고, 원본 사이트에서 게시물 데이터와 메타데이터를 복사하기

이 쿼리에는 다음이 필요합니다.

  • 소스 사이트에 Gato GraphQL + PRO 확장 기능
  • 대상 사이트에 무료 Gato GraphQL 플러그인
  • 대상 사이트의 엔드포인트에서 중첩 뮤테이션 활성화

다음 변수를 제공해야 합니다.

  • postType: 사이트 간에 동기화할 게시물의 커스텀 게시물 유형
  • postSlug: 사이트 간에 동기화할 게시물의 슬러그
  • downstreamServerGraphQLEndpointURL: 대상 WordPress 사이트의 GraphQL 엔드포인트 URL
  • username: 대상 사이트에서 인증하기 위한 애플리케이션 패스워드의 사용자 이름
  • appPassword: 대상 사이트에서 인증하기 위한 애플리케이션 패스워드의 비밀번호
  • update: 기존 게시물을 업데이트하는 경우 true, 새 게시물을 생성하는 경우 false

게시물을 업데이트하는 경우, 업스트림 사이트와 다운스트림 사이트 간의 공통 식별자는 게시물 슬러그입니다.

GraphQL 쿼리는 원본 사이트에서 실행해야 합니다.

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

뉴스레터 구독하기

Gato GraphQL의 모든 업데이트를 놓치지 마세요.