Skip to content

Instantly share code, notes, and snippets.

@leoloso
Created April 15, 2025 23:58
Show Gist options
  • Select an option

  • Save leoloso/1ad1bf835d0aa4e23c4ce33734685746 to your computer and use it in GitHub Desktop.

Select an option

Save leoloso/1ad1bf835d0aa4e23c4ce33734685746 to your computer and use it in GitHub Desktop.
Create posts in bulk using template, for Gato GraphQL
query ExportData {
postTitleTemplate: _echo(value: "Photograph: {label}"
)
@export(as: "postTitleTemplate")
postContentTemplate: _echo(value: """
<!-- wp:paragraph -->
<p>This is a photograph of: <strong>{label}</strong></p>
<!-- /wp:paragraph -->
<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="{URL}" alt="{label}"/></figure>
<!-- /wp:image -->
"""
)
@export(as: "postContentTemplate")
}
query GeneratePostInputs(
$items: [JSONObject!]!
)
@depends(on: "ExportData")
{
postContents: _echo(value: $items)
@underEachArrayItem(
passValueOnwardsAs: "item",
affectDirectivesUnderPos: [1, 2, 3, 4, 5]
)
@applyField(
name: "_objectProperty"
arguments: {
object: $item
by: { key: "label" }
},
passOnwardsAs: "itemLabel"
)
@applyField(
name: "_objectProperty"
arguments: {
object: $item
by: { key: "URL" }
},
passOnwardsAs: "itemURL"
)
@applyField(
name: "_strReplaceMultiple"
arguments: {
search: "{label}"
replaceWith: $itemLabel
in: $postTitleTemplate
},
passOnwardsAs: "postTitle"
)
@applyField(
name: "_strReplaceMultiple"
arguments: {
search: ["{label}", "{URL}"]
replaceWith: [$itemLabel, $itemURL]
in: $postContentTemplate
},
passOnwardsAs: "postContent"
)
@applyField(
name: "_echo"
arguments: {
value: {
status: draft,
contentAs: {
html: $postContent
},
title: $postTitle
}
}
setResultInResponse: true
)
@export(as: "postInputs")
}
mutation CreatePosts
@depends(on: "GeneratePostInputs")
{
createdPostIDs: _echo(value: $postInputs)
@underEachArrayItem(
passValueOnwardsAs: "postInput"
)
@applyField(
name: "createPost"
arguments: {
input: $postInput
},
setResultInResponse: true
)
@export(as: "createdPostIDs")
}
query RetrieveCreatedPosts
@depends(on: "CreatePosts")
{
createdPosts: posts(
filter: {
ids: $createdPostIDs,
status: [draft]
}
) {
id
date
status
content
title
}
}
{
"items": [
{
"label": "Wedding",
"URL": "http://localhost:3000/_next/static/media/GatoGraphQL-logo-suki-text-rectangular.128def99.png"
},
{
"label": "Concert",
"URL": "http://localhost:3000/_next/static/media/GatoGraphQL-logo-suki-rectangular.d3bab7ee.png"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment