Adapting content in bulk

This recipe adapts content in bulk, updating the title and excerpt for multiple posts with a single GraphQL request.

⚙️ Configuration alert:

For this GraphQL query to work, the Schema Configuration applied to the endpoint needs to have Nested Mutations enabled

The GraphQL query below retrieves the data for the multiple posts, executes a search and replace on both title and excerpt fields for each of them, and exports a single dynamic variable $postInputs with all the results as a dictionary, with format:

{
"${post ID}": {
"title": "${adapted post title}",
"excerpt": "${adapted post excerpt}"
},
// repeat for all other posts ...
}

In the mutation operation, each of these entries is then retrieved via _objectProperty (using ${post ID} as the key) and passed as the input to update the post:

query TransformAndExportData(
$limit: Int! = 5,
$offset: Int! = 0,
$replaceFrom: String!
$replaceTo: String!
) {
posts: posts(
pagination: { limit: $limit, offset: $offset }
sort: { by: ID, order: ASC }
) {
title
excerpt
@strReplaceMultiple(
search: $replaceFrom
replaceWith: $replaceTo
affectAdditionalFieldsUnderPos: 1
)
@deferredExport(
as: "postInputs"
type: DICTIONARY
affectAdditionalFieldsUnderPos: 1
)
}
}

mutation UpdatePost(
$limit: Int! = 5,
$offset: Int! = 0
)
@depends(on: "TransformAndExportData")
{
adaptedPosts: posts(
pagination: { limit: $limit, offset: $offset }
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
excerpt
}
}
}
}

🔥 Tips:

Extensions referenced in this recipe permalink

(They are all included in “Application Glue & Automator” Bundle, “Content Translation” Bundle, “All Extensions” Bundle)