Schema 教程第8课:网站迁移
第8课:网站迁移
在将网站迁移到新域名、将页面移动到不同URL等场景下,我们可以执行一批 GraphQL queries 来适配网站内容。
要使此 GraphQL query 正常工作,应用于端点的Schema 配置需要启用嵌套 Mutations。
将内容适配至新域名
此 GraphQL query 首先筛选内容中包含 "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
更改文章或页面的 slug 后,可以将所有内容转换为指向新 URL。
此 GraphQL query 首先从 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 字典提供旧页面 slug 和新页面 slug:
{
"oldPageSlug": "/privacy/",
"newPageSlug": "/user-privacy/"
}