Skip to content

Commit

Permalink
feat(gatsby-source-hacker-news): use schema customization API (#20070)
Browse files Browse the repository at this point in the history
* feat(gatsby-source-hacker-news): use schema customization API

* Use stricter types when possible

* Gatsby dependency bump
  • Loading branch information
vladar authored Dec 16, 2019
1 parent ce09fdc commit e993369
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-source-hacker-news/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ query {
by
descendants
timeISO(fromNow: true)
children {
childrenHnComment {
id
text
timeISO(fromNow: true)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-hacker-news/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.15"
"gatsby": "^2.13.28"
},
"repository": {
"type": "git",
Expand Down
29 changes: 28 additions & 1 deletion packages/gatsby-source-hacker-news/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ const get = query =>
`https://www.graphqlhub.com/graphql?query=${encodeURIComponent(query)}`
)

exports.createSchemaCustomization = async ({ actions }) => {
const typeDefs = `
type HNComment implements Node @childOf(types: ["HNStory", "HNComment"], many: true) {
text: String
timeISO: Date! @dateformat
by: String!
order: Int!
}
type HNStory implements Node {
title: String
score: Int
timeISO: Date! @dateformat
url: String
by: String!
descendants: Int
content: String!
domain: String
order: Int!
}
`
actions.createTypes(typeDefs)
}

exports.sourceNodes = async ({
actions,
getNode,
Expand Down Expand Up @@ -98,7 +122,7 @@ fragment commentsFragment on HackerNewsItem {
kids.kids = []
}
const kidLessStory = _.omit(story, `kids`)
const childIds = kids.kids.map(k => createNodeId(k.id))
const childIds = kids.kids.filter(Boolean).map(k => createNodeId(k.id))

const storyNode = {
...kidLessStory,
Expand All @@ -124,6 +148,9 @@ fragment commentsFragment on HackerNewsItem {
// Recursively create comment nodes.
const createCommentNodes = (comments, parent, depth = 0) => {
comments.forEach((comment, i) => {
if (!comment) {
return
}
if (!comment.kids) {
comment.kids = []
}
Expand Down

0 comments on commit e993369

Please sign in to comment.