스키마 튜토리얼
스키마 튜토리얼레슨 8: 사이트 마이그레이션

레슨 8: 사이트 마이그레이션

새 도메인으로 마이그레이션하거나 다른 URL로 페이지를 이동하는 등의 경우에 GraphQL 쿼리 배치를 실행하여 사이트의 콘텐츠를 적응시킬 수 있습니다.

이 GraphQL 쿼리가 작동하려면 엔드포인트에 적용된 스키마 설정에서 중첩 뮤테이션이 활성화되어 있어야 합니다.

새 도메인에 맞게 콘텐츠 적응시키기

이 GraphQL 쿼리는 먼저 콘텐츠에 "https://my-old-domain.com"이 포함된 모든 게시물을 필터링하고, 해당 문자열을 "https://my-new-domain.com"으로 교체합니다.

mutation ReplaceOldWithNewDomainInPosts {
  posts(
    filter: {
      search: "https://my-old-domain.com"
    }
  ) {
    id
    rawContent
    adaptedRawContent: _strReplace(
      search: "https://my-old-domain.com"
      replaceWith: "https://my-new-domain.com"
      in: $__rawContent
    )
    update(input: {
      contentAs: { html: $__adaptedRawContent }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        rawContent
      }
    }
  }
}

새 게시물 또는 페이지 URL에 맞게 콘텐츠 적응시키기

게시물 또는 페이지의 슬러그를 변경한 후, 모든 콘텐츠가 새 URL을 가리키도록 변환할 수 있습니다.

이 GraphQL 쿼리는 먼저 WordPress의 "siteurl" 설정에서 도메인을 가져와 페이지의 이전 URL과 새 URL을 재구성합니다.

query ExportData(
  $oldPageSlug: String!
  $newPageSlug: String!
) {
  siteURL: optionValue(name: "siteurl")
 
  oldPageURL: _strAppend(
    after: $__siteURL,
    append: $oldPageSlug
  ) @export(as: "oldPageURL")
 
  newPageURL: _strAppend(
    after: $__siteURL,
    append: $newPageSlug
  ) @export(as: "newPageURL")
}
 
mutation ReplaceOldWithNewURLInPosts
  @depends(on: "ExportData")
{
  posts(
    filter: {
      search: $oldPageURL
    },
    sort: { by: ID, order: ASC }
  ) {
    id
    rawContent
    adaptedRawContent: _strReplace(
      search: $oldPageURL
      replaceWith: $newPageURL
      in: $__rawContent
    )
    update(input: {
      contentAs: { html: $__adaptedRawContent }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        rawContent
      }
    }
  }
}

그런 다음 variables 딕셔너리를 통해 이전 페이지 슬러그와 새 페이지 슬러그를 지정합니다.

{
  "oldPageSlug": "/privacy/",
  "newPageSlug": "/user-privacy/"
}