Import posts from CSV

🔥 This query is already included in Gato GraphQL, as Persisted Query “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.

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")
{
createdPostIDs: _echo(value: $postInputs)
@underEachArrayItem(
passValueOnwardsAs: "input"
)
@applyField(
name: "createPost"
arguments: {
input: $input
},
setResultInResponse: true
)
@export(as: "createdPostIDs")
}

query ImportPostsFromCSV
@depends(on: "CreatePostsFromCSVEntries")
{
createdPosts: posts(
filter: {
ids: $createdPostIDs,
status: [draft]
}
) {
id
slug
date
status
title
excerpt
content
author {
id
username
}
}
}

Extensions required for this query permalink

  1. Field on Field permalink

    Manipulate the value of a field by applying some other field on it.

  2. Field To Input permalink

    Retrieve the value of a field, manipulate it, and input it into another field or directive, all within the same operation.

  3. Field Value Iteration and Manipulation permalink

    Iterate and manipulate the value elements of array and object fields.

  4. Helper Function Collection permalink

    Collection of fields providing useful functionality.

  5. HTTP Client permalink

    Addition of fields to execute HTTP requests against a webserver and fetch their response.

  6. Multiple Query Execution permalink

    Combine multiple queries into a single query, sharing state across them and executing them in the requested order.

  7. PHP Functions via Schema permalink

    Manipulate the field output using standard programming language functions available in PHP.

Bundles containing all extensions required for this query permalink

  1. “All in One Toolbox for WordPress” Bundle permalink

    Achieve all superpowers: All of Gato GraphQL extensions, in a single plugin

  2. “Better WordPress Webhooks” Bundle permalink

    Easily create webhooks to process incoming data from any source or service using advanced tools, directly within the wp-admin

  3. “Private GraphQL Server for WordPress” Bundle permalink

    Use GraphQL to power your application (blocks, themes and plugins), internally fetching data without exposing a public endpoint

  4. “Selective Content Import, Export & Sync for WordPress” Bundle permalink

    Import hundreds of records into your WordPress site from another site or service (such as Google Sheets), and selectively export entries to another site

  5. “Tailored WordPress Automator” Bundle permalink

    Create workflows to automate tasks (to transform data, automatically caption images, send notifications, and more)

  6. “Versatile WordPress Request API” Bundle permalink

    Interact with any external API and cloud service, posting and fetching data to/from them

Tutorial lessons explaining how the query works permalink