Queries Library
Queries LibraryTranslate a post from the "Classic editor"

Translate a post from the "Classic editor"

This query translates a "Classic editor" post to the desired language.

Depending on the $update parameter, the translation will either be saved under the same post, or as a new post.

########################################################################
# 
# Variables:
#   - postId: ID of the post to translate
#   - toLang: The language code to translate to, from Google Translate (https://cloud.google.com/translate/docs/languages)
#   - update: Indicate if to update the post, or create a new one
#
# *********************************************************************
#
# === Description ===
#
# This Persisted GraphQL query translates a "Classic editor" post to
# the desired language.
#
# Depending on the `$update` parameter, the translation will either
# be saved as:
#
#   - false (default): A new post (using the translated slug) with "draft" status
#   - true: The same post
#
########################################################################
 
query InitializeVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  isGutenbergEditorEnabled
    @export(as: "isGutenbergEditorEnabled")
}
 
query FetchData($postId: ID!)
  @depends(on: "InitializeVariables")
  @skip(if: $isGutenbergEditorEnabled)
{
  post(by: { id: $postId }, status: any) {
    title
    rawContent
    rawExcerpt
      @export(
        as: "dataToTranslate",
        affectAdditionalFieldsUnderPos: [1, 2]
      )
  }
}
 
query TranslateData(
  $toLang: String!
)
  @depends(on: "FetchData")
  @skip(if: $isGutenbergEditorEnabled)
{  
  translations: _echo(value: $dataToTranslate)
    @underEachJSONObjectProperty
      @strTranslate(to: $toLang)
    @underJSONObjectProperty(by: { key: "title" })
      @export(as: "translatedTitle")
    @underJSONObjectProperty(by: { key: "rawContent" })
      @export(as: "translatedRawContent")
    @underJSONObjectProperty(by: { key: "rawExcerpt" })
      @export(as: "translatedRawExcerpt")
}
 
mutation TranslateClassicEditorPost(
  $postId: ID!
  $update: Boolean! = false
)
  @depends(on: "TranslateData")
  @skip(if: $isGutenbergEditorEnabled)
{
  createPost(input: {
    title: $translatedTitle,
    contentAs: {
      html: $translatedRawContent
    },
    excerpt: $translatedRawExcerpt,
    status: draft
  })
    @skip(if: $update)
  {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      title
      slug
      rawContent
      rawExcerpt
    }    
  }
 
  updatePost(input: {
    id: $postId,
    title: $translatedTitle,
    contentAs: {
      html: $translatedRawContent
    },
    excerpt: $translatedRawExcerpt
  })
    @include(if: $update)
  {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      title
      rawContent
      rawExcerpt
    }    
  }
}