Thư viện QueriesTìm kiếm và thay thế regex nhiều chuỗi trong tất cả bài viết
Tìm kiếm và thay thế regex nhiều chuỗi trong tất cả bài viết
Cập nhật nhiều bài viết chỉ với một thao tác duy nhất, tìm kiếm/thay thế nội dung bằng danh sách các biểu thức chính quy.
Với các bài viết được chỉ định bởi biến $postIds, query này sẽ thay thế tất cả các lần xuất hiện khớp với bất kỳ biểu thức chính quy nào trong $searchRegex bằng chuỗi tương ứng từ $replaceWith, trong tiêu đề, trích đoạn và nội dung của bài viết.
Query này yêu cầu endpoint phải bật Nested Mutations.
query TransformAndExportData(
$postIds: [ID!]!
$searchRegex: [String!]!
$replaceWith: [String!]!
) {
posts: posts(
filter: { ids: $postIds }
pagination: { limit: -1 }
sort: { by: ID, order: ASC }
) {
id
rawTitle
rawContent
rawExcerpt
@strRegexReplaceMultiple(
searchRegex: $searchRegex
replaceWith: $replaceWith
affectAdditionalFieldsUnderPos: [1, 2]
)
@deferredExport(
as: "postAdaptedSources"
type: DICTIONARY
affectAdditionalFieldsUnderPos: [1, 2]
)
}
}
query AdaptDataForMutationInput
@depends(on: "TransformAndExportData")
{
postInputs: _echo(value: $postAdaptedSources)
@underEachJSONObjectProperty(
passValueOnwardsAs: "adaptedSource",
affectDirectivesUnderPos: [1, 2, 3, 4]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawTitle"
}
},
passOnwardsAs: "adaptedTitle"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawExcerpt"
}
},
passOnwardsAs: "adaptedExcerpt"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawContent"
}
},
passOnwardsAs: "adaptedContent"
)
@applyField(
name: "_echo",
arguments: {
value: {
title: $adaptedTitle,
excerpt: $adaptedExcerpt,
contentAs: {
html: $adaptedContent
}
}
},
setResultInResponse: true
)
@export(as: "postInputs")
}
mutation RegexSearchAndReplaceStringsInAllPosts(
$postIds: [ID!]!
)
@depends(on: "AdaptDataForMutationInput")
{
adaptedPosts: posts(
filter: { ids: $postIds }
pagination: { limit: -1 }
sort: { by: ID, order: ASC }
) {
id
postInput: _objectProperty(
object: $postInputs,
by: { key: $__id }
) @remove
update(input: $__postInput) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
title
content
excerpt
}
}
}
}