Thư viện Queries
Thư viện QueriesTìm kiếm và thay thế regex nhiều chuỗi trong một bài viết

Tìm kiếm và thay thế regex nhiều chuỗi trong một bài viết

Query này lấy một bài viết, thay thế tất cả các kết quả khớp của một danh sách chuỗi regex bằng một danh sách chuỗi trong nội dung và tiêu đề của bài viết, sau đó lưu lại bài viết.

query GetPostData(
  $postId: ID!
  $searchRegex: [String!]!,
  $replaceWith: [String!]!
) {
  post(by: { id: $postId }, status: any) {
    title
    adaptedPostTitle: _strRegexReplaceMultiple(
      searchRegex: $searchRegex
      replaceWith: $replaceWith
      in: $__title
    )
      @export(as: "adaptedPostTitle")
 
    rawContent
    adaptedRawContent: _strRegexReplaceMultiple(
      searchRegex: $searchRegex
      replaceWith: $replaceWith
      in: $__rawContent
    )
      @export(as: "adaptedRawContent")
  }
}
 
mutation RegexSearchAndReplaceStringsInPost($postId: ID!)
  @depends(on: "GetPostData")
{
  updatePost(input: {
    id: $postId,
    title: $adaptedPostTitle,
    contentAs: { html: $adaptedRawContent },
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      title
      rawContent
    }
  }
}