Queries Library
Queries LibraryInject data from CSV into a Bricks post

Inject data from CSV into a Bricks post

This query parses CSV data and injects it into the text elements in a Bricks page.

This query requires the Bricks extension to be enabled.

The query requires the following variables:

  • $customPostId: The ID of the Bricks custom post to update
  • $csvFileURL: The URL of the CSV file to parse
query InitializeGlobalVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  emptyArray: _echo(value: [])
    @export(as: "elementToUpdateIDs")
  
  emptyNumber: _echo(value: 0)
    @export(as: "numberCsvEntries")
}
 
query GetCSVData(
  $url: URL!
  $headingElementColumn: String! = "Title"
  $textElementColumn: String! = "Description"
)
  @depends(on: "InitializeGlobalVariables")
{
  _sendHTTPRequest(input: {
    url: $url,
    method: GET
  }) {
    body
    csv: _strParseCSV(
      string: $__body
    )
      @underEachArrayItem(
        passValueOnwardsAs: "csvPostEntry"
        affectDirectivesUnderPos: [1, 3]
      )
        @underJSONObjectProperty(by: { key: $headingElementColumn })
          @export(as: "csvHeadings")
        @underJSONObjectProperty(by: { key: $textElementColumn })
          @export(as: "csvTexts")
    numberCsvEntries: _arrayLength(array: $__csv)
      @export(as: "numberCsvEntries")
  }
}
 
query ExportData($customPostId: ID!)
  @depends(on: "GetCSVData")
{
  customPost(by:{ id: $customPostId }, status: any) {
    id
    title
    bricksDataTextElements: bricksData(filterBy: { include: ["text"] })
      @arraySplice(offset: $numberCsvEntries)
      @underEachArrayItem(
        passIndexOnwardsAs: "index",
        passValueOnwardsAs: "elementJSON"
        affectDirectivesUnderPos: [1, 2, 3]
      )
        @applyField(
          name: "_objectProperty",
          arguments: {
            object: $elementJSON,
            by: { key: "id" }
          },
          passOnwardsAs: "elementID"
        )
        @applyField(
          name: "_arrayItem",
          arguments: {
            array: $csvTexts,
            position: $index
          },
          passOnwardsAs: "csvText"
        )
        @applyField(
          name: "_echo",
          arguments: {
            value: {
              id: $elementID,
              settings: {
                text: $csvText
              }
            }
          }
          setResultInResponse: true
        )
      @export(as: "textMergeInputElements")
    bricksDataHeadingElements: bricksData(filterBy: { include: ["heading"] })
      @arraySplice(offset: $numberCsvEntries)
      @underEachArrayItem(
        passIndexOnwardsAs: "index",
        passValueOnwardsAs: "elementJSON"
        affectDirectivesUnderPos: [1, 2, 3]
      )
        @applyField(
          name: "_objectProperty",
          arguments: {
            object: $elementJSON,
            by: { key: "id" }
          },
          passOnwardsAs: "elementID"
        )
        @applyField(
          name: "_arrayItem",
          arguments: {
            array: $csvHeadings,
            position: $index
          },
          passOnwardsAs: "csvHeading"
        )
        @applyField(
          name: "_echo",
          arguments: {
            value: {
              id: $elementID,
              settings: {
                text: $csvHeading
              }
            }
          }
          setResultInResponse: true
        )
      @export(as: "headingMergeInputElements")
  }
}
 
query AdaptData
  @depends(on: "ExportData")
{
  allMergeInputElements: _arrayMerge(
    arrays: [$textMergeInputElements, $headingMergeInputElements]
  )
    @export(as: "allMergeInputElements")
}
 
mutation UpdateData($customPostId: ID!)
  @depends(on: "AdaptData")
{
  bricksMergeCustomPostElementDataItem(input: {
    customPostID: $customPostId
    elements: $allMergeInputElements
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
          @passOnwards(as: "message")
          @fail(
            message: $message
            condition: ALWAYS
          )
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        bricksData
      }
    }
  }
}