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: add node links and backreferences during node creation #1

Closed
wants to merge 24 commits into from

Conversation

nocash
Copy link

@nocash nocash commented Dec 17, 2018

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'),
          })
      })

      // ...
    })
  })
}

This will allow for working with tags and users in Gatsby that do not
yet have posts associated with them.
From the docs:
> Since `[email protected]`, 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
@nocash nocash changed the title add node links and backreferences during node creation wip: add node links and backreferences during node creation Dec 17, 2018
@nocash nocash changed the base branch from develop to master December 17, 2018 22:53
@nocash nocash force-pushed the create-node-references branch from c011adb to 2e9117f Compare December 19, 2018 04:15
@nocash nocash force-pushed the create-node-references branch from d291e89 to 48bb89b Compare December 19, 2018 19:02
@nocash nocash force-pushed the create-node-references branch from 37d4ac1 to 9d94070 Compare December 19, 2018 19:35
@nocash nocash closed this Jan 11, 2019
@nocash nocash deleted the create-node-references branch January 11, 2019 20:20
@nocash nocash restored the create-node-references branch January 14, 2019 22:28
@nocash nocash reopened this Jan 14, 2019
@nocash
Copy link
Author

nocash commented Jan 15, 2019

Closing this in favor of #2 (uses develop branch as base).

@nocash nocash closed this Jan 15, 2019
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