Queries Library
Queries LibraryTranslate posts (in bulk) from the "Classic editor"

Translate posts (in bulk) from the "Classic editor"

This query translates multiple "Classic editor" posts at once (in bulk), while executing a single call to the Google Translate API containing all text to translate from all the posts.

This query requires the endpoint to have Nested Mutations enabled.

query FetchData($postIds: [ID!]!)
{
  posts(filter: { ids: $postIds, status: any } ) {
    title
    rawContent
    rawExcerpt
      @export(
        as: "dataToTranslate",
        affectAdditionalFieldsUnderPos: [1, 2]
        type: DICTIONARY
      )
  }
}
 
query TranslateData(
  $toLang: String!
)
  @depends(on: "FetchData")
{  
  translatedData: _echo(value: $dataToTranslate)
    @underEachJSONObjectProperty
      @underEachJSONObjectProperty
        @strTranslate(to: $toLang)
    @export(as: "translatedData")
}
 
query GenerateMutationInputs
  @depends(on: "TranslateData")
{  
  postInputs: _echo(value: $translatedData)
    @underEachJSONObjectProperty(
      passValueOnwardsAs: "postTranslatedData"
      affectDirectivesUnderPos: [1, 2, 3, 4]
    )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $postTranslatedData,
          by: {
            key: "title",
          }
        },
        passOnwardsAs: "postTranslatedTitle"
      )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $postTranslatedData,
          by: {
            key: "rawExcerpt",
          }
        },
        passOnwardsAs: "postTranslatedRawExcerpt"
      )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $postTranslatedData,
          by: {
            key: "rawContent",
          }
        },
        passOnwardsAs: "postTranslatedRawContent"
      )
      @applyField(
        name: "_echo",
        arguments: {
          value: {
            title: $postTranslatedTitle,
            excerpt: $postTranslatedRawExcerpt,
            contentAs: {
              html: $postTranslatedRawContent
            }
          }
        },
        setResultInResponse: true
      )
    @export(as: "postInputs")
}
 
mutation TranslateClassicEditorPosts($postIds: [ID!]!)
  @depends(on: "GenerateMutationInputs")
{
  updatePosts: posts(filter: { ids: $postIds, status: any } ) {
    id
    postInput: _objectProperty(
      object: $postInputs,
      by: {
        key: $__id
      }
    )
      @remove
    update(input: $__postInput) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        title
        rawExcerpt
        rawContent
      }
    }
  }
}