Thư viện Queries
Thư viện QueriesThay thế các phần tử văn bản trong trang Bricks bằng nội dung được tiêm

Thay thế các phần tử văn bản trong trang Bricks bằng nội dung được tiêm

Query này thay thế các phần tử text trong trang Bricks bằng dữ liệu được tiêm qua các biến.

Query này yêu cầu tiện ích mở rộng Bricks phải được kích hoạt.

Query yêu cầu các biến sau:

  • $customPostId: ID của bài đăng tùy chỉnh Bricks cần cập nhật
  • $descriptions: Một mảng chuỗi để thay thế các phần tử text
query InitializeGlobalVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  emptyArray: _echo(value: [])
    @export(as: "elementToUpdateIDs")
}
 
query ExportData($customPostId: ID!)
  @depends(on: "InitializeGlobalVariables")
{
  customPost(by:{ id: $customPostId }, status: any) {
    id
    title
    bricksData(filterBy: { include: ["text"] })
      @underEachArrayItem
        @underJSONObjectProperty(by: { key: "id" })
          @export(as: "elementToUpdateIDs")
  }
}
 
query ProcessData($descriptions: [String!]!)
  @depends(on: "ExportData")
{  # Make sure the number of descriptions is the same as the number of elements to update
  numberOfDescriptions: _arrayLength(array: $elementToUpdateIDs)
  descriptions: _arrayPad(array: $descriptions, length: $__numberOfDescriptions, value: "")
    @export(as: "adaptedDescriptions")
}
 
query AdaptData
  @depends(on: "ProcessData")
{
  elementToUpdateIDs: _echo(value: $elementToUpdateIDs)
  elementToUpdateTexts: _echo(value: $adaptedDescriptions)
  elementToUpdateMergeInputElements: _echo(value: $elementToUpdateIDs)
    @underEachArrayItem(
      passIndexOnwardsAs: "index",
      passValueOnwardsAs: "elementToUpdateID"
      affectDirectivesUnderPos: [1, 2]
    )
      @applyField(
        name: "_arrayItem",
        arguments: {
          array: $adaptedDescriptions,
          position: $index
        },
        passOnwardsAs: "elementToUpdateText"
      )
      @applyField(
        name: "_echo",
        arguments: {
          value: {
            id: $elementToUpdateID,
            settings: {
              text: $elementToUpdateText
            }
          }
        }
        setResultInResponse: true
      )
    @export(as: "elementToUpdateMergeInputElements")
}
 
mutation UpdateData($customPostId: ID!)
  @depends(on: "AdaptData")
{
  bricksMergeCustomPostElementDataItem(input: {
    customPostID: $customPostId
    elements: $elementToUpdateMergeInputElements
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
          @passOnwards(as: "message")
          @fail(
            message: $message
            condition: ALWAYS
          )
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        bricksData
      }
    }
  }
}