Queries Library
Queries LibraryRegex search and replace multiple strings in all posts

Regex search and replace multiple strings in all posts

Update multiple posts with a single operation, searching/replacing content using a list of regular expressions.

Given the posts indicated by variable $postIds, this query will replace all occurrences matching any of the regular expressions in $searchRegex with the corresponding string from $replaceWith, in the post's title, excerpt and content.

This query requires the endpoint to have Nested Mutations enabled.

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