Queries Library
Queries LibraryImport post from WordPress RSS feed

Import post from WordPress RSS feed

This query imports a post from a WordPress RSS feed, using the title, content and excerpt of the post.

If the author with that username exists locally, it uses it, otherwise it replaces it with the one defined via variable $defaultAuthorUsername.

Variable $url receives the URL of the WordPress single post's RSS feed. It usually is the blog post URL + "/feed/rss/?withoutcomments=1". Eg:

https://mysite.com/blog/hello-world/feed/rss/?withoutcomments=1
query GetPostFromRSSFeedAndExportData(
  $url: URL!
) {
  _sendHTTPRequest(input: {
    url: $url,
    method: GET
  }) {
    body
    rssJSON: _strDecodeXMLAsJSON(
      xml: $__body
      alwaysArrayTagNames: [
        "category",
      ],
    )
 
    # Fields to be imported
    authorUsername: _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.dc:creator"
      }
    )
      @export(as: "authorUsername")
 
    # categorySlugs: _objectProperty(
    #   object: $__rssJSON,
    #   by: {
    #     path: "rss.channel.item.category"
    #   }
    # )
 
    content:  _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.content:encoded"
      }
    )
      @export(as: "content")
 
    excerpt:  _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.description"
      }
    )
      @export(as: "excerpt")
 
    title:  _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.title"
      }
    )
      @export(as: "title")
  }
}
 
# If the author's username exists in this site, keep it
# Otherwise, use the default one
query CheckAuthorExistsOrChange(
  $defaultAuthorUsername: String! = "admin"
)
  @depends(on: "GetPostFromRSSFeedAndExportData")
{
  existingUserByUsername: user(by: { username: $authorUsername })
  {
    id
    username
  }
  userExists: _notNull(value: $__existingUserByUsername)
  username: _if(
    condition: $__userExists,
    then: $authorUsername,
    else: $defaultAuthorUsername
  )
    @export(as: "existingAuthorUsername")
}
 
mutation ImportPostFromWPRSSFeed
  @depends(on: "CheckAuthorExistsOrChange")
{
  createPost(input: {
    status: draft,
    authorBy: {
      username: $existingAuthorUsername
    },
    contentAs: {
      html: $content
    },
    excerpt: $excerpt
    title: $title
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      slug
      date
      status
 
      author {
        id
        username
      }
      content
      excerpt
      title
    }
  }
}