쿼리 라이브러리
쿼리 라이브러리"Classic editor"에서 게시물을 (일괄) 번역하기

"Classic editor"에서 게시물을 (일괄) 번역하기

이 쿼리는 여러 「Classic editor」 게시물을 한 번에 (일괄로) 번역합니다. 모든 게시물에서 번역할 텍스트를 모아 Google Translate API를 한 번만 호출하여 처리합니다.

이 쿼리를 사용하려면 엔드포인트에서 중첩 Mutation이 활성화되어 있어야 합니다.

########################################################################
# 
# Variables:
#   - postIds: List of IDs of the posts to translate
#   - toLang: The language code to translate to, from Google Translate (https://cloud.google.com/translate/docs/languages)
#
# *********************************************************************
#
# === Description ===
#
# This Persisted GraphQL 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.
#
# The translation for each post will be saved under the same post.
#
# See Persisted Query "Translate post (Classic editor)" for additional
# documentation.
#
########################################################################
 
query InitializeVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  isGutenbergEditorEnabled
    @export(as: "isGutenbergEditorEnabled")
}
 
query FetchData($postIds: [ID!]!)
  @depends(on: "InitializeVariables")
  @skip(if: $isGutenbergEditorEnabled)
{
  posts(filter: { ids: $postIds, status: any } ) {
    title
    rawContent
    rawExcerpt
      @export(
        as: "dataToTranslate",
        affectAdditionalFieldsUnderPos: [1, 2]
        type: DICTIONARY
      )
  }
}
 
query TranslateData(
  $toLang: String!
)
  @depends(on: "FetchData")
  @skip(if: $isGutenbergEditorEnabled)
{  
  translatedData: _echo(value: $dataToTranslate)
    @underEachJSONObjectProperty
      @underEachJSONObjectProperty
        @strTranslate(to: $toLang)
    @export(as: "translatedData")
}
 
query GenerateMutationInputs
  @depends(on: "TranslateData")
  @skip(if: $isGutenbergEditorEnabled)
{  
  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")
  @skip(if: $isGutenbergEditorEnabled)
{
  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
      }
    }
  }
}