Hướng dẫn schema
Hướng dẫn schemaBài 8: Di chuyển trang web

Bài 8: Di chuyển trang web

Chúng ta có thể thực thi một loạt các queries GraphQL để điều chỉnh nội dung trên trang web khi di chuyển sang tên miền mới, chuyển các trang sang URL khác, hoặc các tác vụ tương tự.

Để queries GraphQL này hoạt động, Cấu hình Schema được áp dụng cho endpoint cần bật tính năng Nested Mutations

Điều chỉnh nội dung theo tên miền mới

Queries GraphQL này trước tiên lọc tất cả các bài viết có chứa "https://my-old-domain.com" trong nội dung, và thay thế chuỗi này bằng "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
      }
    }
  }
}

Điều chỉnh nội dung theo URL bài viết hoặc trang mới

Sau khi thay đổi slug của một bài viết hoặc trang, chúng ta có thể chuyển đổi toàn bộ nội dung để trỏ đến URL mới.

Queries GraphQL này trước tiên lấy tên miền từ cài đặt WordPress "siteurl" để tái tạo URL cũ và mới của trang:

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

Sau đó chúng ta cung cấp slug cũ và mới của trang thông qua từ điển variables:

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