Queries Library
Queries LibraryImport posts from CSV

Import posts from CSV

This query imports posts from a CSV.

It creates a new post with the title, excerpt, content and author (provided via username) of each post.

The names of the columns with those fields are provided via variables:

  • $titleColumn
  • $excerptColumn
  • $contentColumn
  • $authorUsernameColumn

...with default values "Title", "Excerpt", "Content" and "Author" respectively.

The URL of the CSV source must be provided via variable $url.

########################################################################
# 
# Variables:
#   - url: URL of the CSV
#   - titleColumn: Column name (on the CSV header) for the post's title. Default: "Title"
#   - excerptColumn: Column name (on the CSV header) for the post's excerpt. Default: "Excerpt"
#   - contentColumn: Column name (on the CSV header) for the post's content. Default: "Content"
#   - authorUsernameColumn: Column name (on the CSV header) for the post author's username. Default: "Author"
#
# *********************************************************************
#
# === Description ===
#
# This Persisted GraphQL query imports posts from a CSV.
#
# It creates a new post with the title, excerpt, content and author
# (provided via username) of each post.
#
# The names of the columns with those fields are provided via vars,
# with default values "Title", "Excerpt", "Content" and "Author"
# respectively.
#
########################################################################
#
# === CSV Examples ===
#
# The following CSV will import 3 posts:
# 
#     Title,Excerpt,Content,Author
#     Multi-channelled optimal interface,Inventore iste velit et expedita ipsa dolor,<p>Inventore iste velit et expedita ipsa dolor. Optio suscipit qui id harum corporis dignissimos.</p>,admin
#     Ergonomic motivating parallelism,Consequuntur velit quasi assumenda,<p>Consequuntur velit quasi assumenda. Eum non saepe est. Ut dolorem harum eveniet eaque nemo impedit. Voluptatem commodi modi sed sed animi voluptatem.</p>,admin
#     Down-sized solution-oriented securedline,Ipsam minima qui asperiores dolorum unde voluptas quia voluptate,<p>Ipsam minima qui asperiores dolorum unde voluptas quia voluptate. Incidunt omnis cumque beatae quo deleniti ex. Praesentium nihil et consectetur neque commodi tempora ipsa adipisci. Praesentium perferendis sint est.</p>,admin
#
# The content can contain newlines (then it must be surrounded with `"`),
# and block markup (to insert posts for the WordPress editor):
# 
#     Title,Excerpt,Content,Author
#     Multi-channelled optimal interface,Inventore iste velit et expedita ipsa dolor,"<p>Inventore iste velit et expedita ipsa dolor. Optio suscipit qui id harum corporis dignissimos.</p>
#     <p>Iste provident aut incidunt dolorum esse nobis. Porro est deserunt quos quia cupiditate earum magnam. Distinctio qui et aut iste et.</p>
#     <p>Aut earum quos accusamus voluptas sit tempore est. Dolore tempora doloremque magni excepturi. Ullam illum et ullam cum sed.</p>",admin
#     Ergonomic motivating parallelism,Consequuntur velit quasi assumenda,"<p>Consequuntur velit quasi assumenda. Eum non saepe est. Ut dolorem harum eveniet eaque nemo impedit. Voluptatem commodi modi sed sed animi voluptatem.</p>
#     <figure class=""wp-block-image size-large""><img src=""https://mysite.com/wp-content/uploads/wordpress0.jpg"" class=""wp-image-5""/></figure>
#     <p>Velit quo iste omnis sint quis. Voluptatibus dolorem sequi ipsum corrupti officia. Ut rem quasi ipsa ut. Ut ad dolor ullam error excepturi.</p>",admin
#     Down-sized solution-oriented securedline,Ipsam minima qui asperiores dolorum unde voluptas quia voluptate,"<p>Ipsam minima qui asperiores dolorum unde voluptas quia voluptate. Incidunt omnis cumque beatae quo deleniti ex. Praesentium nihil et consectetur neque commodi tempora ipsa adipisci. Praesentium perferendis sint est.</p>
#     <p>Et aut fuga aliquam consequuntur dolore sunt ut ut. Quibusdam in voluptas vitae quia eaque. Eligendi dolorum dolorem vitae et vitae et ut.</p>",admin
#
# The content can also contain block markup, to insert posts for the
# WordPress editor:
#
#     Title,Excerpt,Content,Author
#     Ergonomic motivating parallelism,Consequuntur velit quasi assumenda,"<!-- wp:paragraph -->
#     <p>Consequuntur velit quasi assumenda. Eum non saepe est. Ut dolorem harum eveniet eaque nemo impedit. Voluptatem commodi modi sed sed animi voluptatem.</p>
#     <!-- /wp:paragraph -->
#     <!-- wp:image {""id"":5,""sizeSlug"":""large""} -->
#     <figure class=""wp-block-image size-large""><img src=""https://mysite.com/wp-content/uploads/wordpress0.jpg"" alt=""wordpress logo"" class=""wp-image-5""/></figure>
#     <!-- /wp:image -->",admin
#
########################################################################
 
query InitializeDynamicVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  postInputs: _echo(value: [])
    @export(as: "postInputs")
    @remove
}
 
query GetPostsFromCSVAndExportData(
  $url: URL!
  $titleColumn: String! = "Title"
  $excerptColumn: String! = "Excerpt"
  $contentColumn: String! = "Content"
  $authorUsernameColumn: String! = "Author"
)
  @depends(on: "InitializeDynamicVariables")
{
  _sendHTTPRequest(input: {
    url: $url,
    method: GET
  }) {
    body
    csv: _strParseCSV(
      string: $__body
    )
    postInputs: _echo(value: $__csv)
      @underEachArrayItem(
        passValueOnwardsAs: "csvPostEntry"
        affectDirectivesUnderPos: [1, 2, 3, 4, 5]
      )
        @applyField(
          name: "_objectProperty",
          arguments: {
            object: $csvPostEntry,
            by: {
              key: $titleColumn,
            }
          },
          passOnwardsAs: "postTitle"
        )
        @applyField(
          name: "_objectProperty",
          arguments: {
            object: $csvPostEntry,
            by: {
              key: $excerptColumn,
            }
          },
          passOnwardsAs: "postExcerpt"
        )
        @applyField(
          name: "_objectProperty",
          arguments: {
            object: $csvPostEntry,
            by: {
              key: $contentColumn,
            }
          },
          passOnwardsAs: "postContent"
        )
        @applyField(
          name: "_objectProperty",
          arguments: {
            object: $csvPostEntry,
            by: {
              key: $authorUsernameColumn,
            }
          },
          passOnwardsAs: "postAuthorUsername"
        )
        # Already create (and export) the inputs for the mutation
        @applyField(
          name: "_echo",
          arguments: {
            value: {
              status: draft,
              title: $postTitle,
              excerpt: $postExcerpt,
              contentAs: {
                html: $postContent
              },
              authorBy: {
                username: $postAuthorUsername
              }
            }
          },
          setResultInResponse: true
        )
      @export(as: "postInputs")
  }
}
 
mutation CreatePostsFromCSVEntries
  @depends(on: "GetPostsFromCSVAndExportData")
{
  createPosts(inputs: $postInputs) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      slug
      date
      status
      title
      excerpt
      content
      author {
        id
        username
      }
    }
  }
}