Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: new features for Ghost source plugin #2

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

wip: new features for Ghost source plugin #2

wants to merge 32 commits into from

Conversation

nocash
Copy link

@nocash nocash commented Jan 15, 2019

Rewrites a large portion of the plugin in order to properly link related values to their GraphQL node objects.

Todo

  • Remove the GhostPage node type (Note: the GhostPage node type has been readded after the updates for the v2 API).
  • Use createRemoteFileNode to copy images to local file system
  • Add Babel and target Node 4
  • Add Prettier config to maintain code style consistency
  • Replace the large "does roughly the right thing" test with smaller, focused tests
  • Update README

Significant changes

  • Replaced certain object properties with links to the related node(s). post.tags, post.primary_tag, post.authors, and post.primary_author now return the actual nodes rather than containing a limited copy of the data.

  • Removes the GhostPage node type (explanation below). (Note: the GhostPage node type has been readded after the updates for the v2 API).

  • Added a posts collection to GhostTag and GhostAuthor nodes that contain node references to the posts they are assigned to (e.g. author.posts returns all posts written by that user).

  • Adds a GhostMedia node type that is used for post.feature_image, tag.feature_image, user.profile_image, and user.cover_image. The GhostMedia type contains a src field the retains the original URL from those fields but additionally downloads the image and creates a localFile node that can be used with the gatsby-image package.

  • Queries the /tags and /users endpoints of the Ghost API to get a full set of data rather than inferring it from the posts collection. Previously, tags that were not added to any posts were never passed to Gatsby, and the same was true for users.

Removing the GhostPage node type

Note: The GhostPage node type was removed when working with the v0.1 API but has been readded after updating to the v2 API to maintain parity with the API resources (see https://docs.ghost.org/api/content/#resources). The information below is being kept for posterity but may be removed later.

GhostPage is not significantly different from GhostPost— the only difference being that post.page is true instead of false— and now that nodes and backreferences are working correctly it complicates what should be trivial queries by requiring inline fragments in order to select fields from otherwise identical objects. E.g.:

query {
  ghostTag(id: {eq: "..."}) {
    name
    posts {
      ... on GhostPost {
        title
      }
      ... on GhostPage {
        title
      }
    }
  }
}

We can remove the GhostPage type from the source plugin and we leave it to the consumers to decide how they want to handle pages. This is easily done in gatsby-node.js:

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions
  return new Promise((resolve, reject) => {
    graphql(`
      {
        allGhostPost {
          edges {
            node {
              slug
              page
            }
          }
        }
    `).then(result => {
      result.data.allGhostPost.edges
        .filter(({node}) => !node.page)
        .forEach(({ node }) => {
          createPage({
            path: `post/${node.slug}`,
            component: path.resolve('./src/templates/post.tsx'),
          })
      })

      result.data.allGhostPost.edges
        .filter(({node}) => node.page)
        .forEach(({ node }) => {
          createPage({
            path: `page/${node.slug}`,
            component: path.resolve('./src/templates/page.tsx'),
          })
      })

      // ...
    })
  })
}

nocash and others added 26 commits December 16, 2018 17:30
This will allow for working with tags and users in Gatsby that do not
yet have posts associated with them.
From the docs:
> Since `sinon@5.0.0`, the `sinon` object is a default sandbox. Unless
> you have a very advanced setup or need a special configuration, you
> probably want to just use that one.

https://sinonjs.org/releases/v7.2.2/sandbox/#default-sandbox
This was done originally in 714bdc7 but I missed it during the merge.
@nocash
Copy link
Author

nocash commented Jan 15, 2019

The changes included have outgrown the scope of #1 so I'm closing it in favor of this PR.

nocash pushed a commit that referenced this pull request Jan 15, 2019
closes #2

- This is the hacky workaround recommended by Gatsby
- Ref: gatsbyjs/gatsby#10856 (comment)
- It depends on private API, but is better than needing a data stub!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants