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

[1.0] refactor node structure to hide internal fields #960

Merged
merged 6 commits into from
May 15, 2017
Next Next commit
Initial refactor done
  • Loading branch information
KyleAMathews committed May 13, 2017
commit 02d4c955f117977125b3b0da5b367d1531ceb56f
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function queueImageResizing({ file, args = {} }) {
return true
})
const sortedArgs = _.sortBy(filteredArgs, arg => arg[0] === `width`)
const imgSrc = `/${file.contentDigest}-${qs.stringify(_.fromPairs(sortedArgs))}.${file.extension}`
const imgSrc = `/${file.internal.contentDigest}-${qs.stringify(_.fromPairs(sortedArgs))}.${file.extension}`
const filePath = `${process.cwd()}/public${imgSrc}`
// Create function to call when the image is finished.
let outsideResolve
Expand Down
38 changes: 19 additions & 19 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const makeTypeName = type => {
const processEntities = ents => {
return ents.map(ent => {
const newEnt = {
...ent.attributes,
id: ent.id,
type: ent.type,
internal: {
type: ent.type,
},
...ent.attributes,
created: new Date(ent.attributes.created * 1000).toJSON(),
changed: new Date(ent.attributes.changed * 1000).toJSON(),
}
Expand Down Expand Up @@ -43,13 +45,6 @@ exports.sourceNodes = async (
})

// Touch existing Drupal nodes so Gatsby doesn't garbage collect them.
// console.log(
// "existing drupal nodes",
// _.values(store.getState().nodes)
// .filter(n => n.type.slice(0, 8) === `drupal__`)
// .map(n => n.id)
// )

_.values(store.getState().nodes)
.filter(n => n.type.slice(0, 8) === `drupal__`)
.forEach(n => touchNode(n.id))
Expand Down Expand Up @@ -94,12 +89,15 @@ exports.sourceNodes = async (

const gatsbyNode = {
...node,
parent: `__SOURCE__`,
type: makeTypeName(node.type),
children: [],
content: nodeStr,
parent: `__SOURCE__`,
internal: {
...node.internal,
type: makeTypeName(node.internal.type),
content: nodeStr,
mediaType: `application/json`,
},
author___NODE: result.data.data[i].relationships.uid.data.id,
mediaType: `application/json`,
}

// Get content digest of node.
Expand All @@ -108,7 +106,7 @@ exports.sourceNodes = async (
.update(JSON.stringify(gatsbyNode))
.digest(`hex`)

gatsbyNode.contentDigest = contentDigest
gatsbyNode.internal.contentDigest = contentDigest

createNode(gatsbyNode)
})
Expand All @@ -124,11 +122,13 @@ exports.sourceNodes = async (

const gatsbyUser = {
...user,
parent: `__SOURCE__`,
type: makeTypeName(user.type),
children: [],
content: userStr,
mediaType: `application/json`,
parent: `__SOURCE__`,
internal: {
type: makeTypeName(user.type),
content: userStr,
mediaType: `application/json`,
},
}

if (gatsbyUser.uid === 1) {
Expand All @@ -150,7 +150,7 @@ exports.sourceNodes = async (
.update(JSON.stringify(gatsbyUser))
.digest(`hex`)

gatsbyUser.contentDigest = contentDigest
gatsbyUser.internal.contentDigest = contentDigest

createNode(gatsbyUser)

Expand Down
10 changes: 6 additions & 4 deletions packages/gatsby-source-filesystem/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ function readFile(file, pluginOptions, cb) {
// people will use the id for that and ids shouldn't be treated as
// useful information.
id: createId(file),
contentDigest: contentDigest,
children: [],
parent: `___SOURCE___`,
mediaType: mime.lookup(slashedFile.ext),
type: `File`,
sourceName: pluginOptions.name,
internal: {
contentDigest: contentDigest,
mediaType: mime.lookup(slashedFile.ext),
type: `File`,
},
sourceInstanceName: pluginOptions.name,
absolutePath: slashedFile.absolutePath,
relativePath: slash(
path.relative(pluginOptions.path, slashedFile.absolutePath)
Expand Down
28 changes: 16 additions & 12 deletions packages/gatsby-source-hacker-news/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ fragment commentsFragment on HackerNewsItem {

const storyNode = {
...kidLessStory,
domain,
order: i + 1,
parent: `__SOURCE__`,
type: `HNStory`,
children: [...kids.kids.map(k => k.id)],
Copy link
Contributor

@0x80 0x80 May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of the PR but I just happened to notice the redundant [...] here. It could just be children: kids.kids.map(k => k.id),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good point :-)

parent: `__SOURCE__`,
content: storyStr,
mediaType: `application/json`,
internal: {
type: `HNStory`,
mediaType: `application/json`,
},
domain,
order: i + 1,
}

// Just store the user id
Expand All @@ -121,7 +123,7 @@ fragment commentsFragment on HackerNewsItem {
.update(JSON.stringify(storyNode))
.digest(`hex`)

storyNode.contentDigest = contentDigest
storyNode.internal.contentDigest = contentDigest

createNode(storyNode)

Expand All @@ -133,11 +135,13 @@ fragment commentsFragment on HackerNewsItem {
}
let commentNode = {
..._.omit(comment, `kids`),
order: i + 1,
type: `HNComment`,
parent,
children: [...comment.kids.map(k => k.id)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

mediaType: `application/json`,
parent,
internal: {
type: `HNComment`,
mediaType: `application/json`,
},
order: i + 1,
}

commentNode.by = commentNode.by.id
Expand All @@ -149,8 +153,8 @@ fragment commentsFragment on HackerNewsItem {
.update(nodeStr)
.digest(`hex`)

commentNode.contentDigest = contentDigest
commentNode.content = nodeStr
commentNode.internal.contentDigest = contentDigest
commentNode.internal.content = nodeStr

createNode(commentNode)

Expand Down
22 changes: 12 additions & 10 deletions packages/gatsby-transformer-json/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
// but since this transformer creates new nodes with the same media-type
// as its parent node, we have to add this check that we didn't create
// the node).
if (node.pluginName === `gatsby-transformer-json`) {
if (node.internal.pluginName === `gatsby-transformer-json`) {
return
}

// We only care about JSON content.
if (node.mediaType !== `application/json`) {
if (node.internal.mediaType !== `application/json`) {
return
}

Expand All @@ -28,15 +28,17 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
return {
...obj,
id: obj.id ? obj.id : `${node.id} [${i}] >>> JSON`,
contentDigest,
mediaType: `application/json`,
parent: node.id,
// TODO make choosing the "type" a lot smarter. This assumes
// the parent node is a file.
// PascalCase
type: _.upperFirst(_.camelCase(`${node.name} Json`)),
children: [],
content: objStr,
parent: node.id,
internal: {
contentDigest,
mediaType: `application/json`,
// TODO make choosing the "type" a lot smarter. This assumes
// the parent node is a file.
// PascalCase
type: _.upperFirst(_.camelCase(`${node.name} Json`)),
content: objStr,
},
}
})

Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const prune = require(`underscore.string/prune`)

let pluginsCacheStr = ``
const astCacheKey = node => {
return `transformer-remark-markdown-ast-${node.contentDigest}-${pluginsCacheStr}`
return `transformer-remark-markdown-ast-${node.internal.contentDigest}-${pluginsCacheStr}`
}
const htmlCacheKey = node => {
return `transformer-remark-markdown-html-${node.contentDigest}-${pluginsCacheStr}`
return `transformer-remark-markdown-html-${node.internal.contentDigest}-${pluginsCacheStr}`
}
const headingsCacheKey = node => {
return `transformer-remark-markdown-headings-${node.contentDigest}-${pluginsCacheStr}`
return `transformer-remark-markdown-headings-${node.internal.contentDigest}-${pluginsCacheStr}`
}

module.exports = (
Expand Down Expand Up @@ -68,7 +68,7 @@ module.exports = (
}
})
).then(() => {
const markdownAST = remark.parse(markdownNode.content)
const markdownAST = remark.parse(markdownNode.internal.content)

// source => parse (can order parsing for dependencies) => typegen
//
Expand Down
18 changes: 10 additions & 8 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ module.exports = async function onNodeCreate({
// but since this transformer creates new nodes with the same media-type
// as its parent node, we have to add this check that we didn't create
// the node).
if (node.type === `MarkdownRemark`) {
if (node.internal.type === `MarkdownRemark`) {
return
}

// We only care about markdown content.
if (node.mediaType !== `text/x-markdown`) {
if (node.internal.mediaType !== `text/x-markdown`) {
return
}

Expand All @@ -30,12 +30,14 @@ module.exports = async function onNodeCreate({
.digest(`hex`)
const markdownNode = {
id: `${node.id} >>> MarkdownRemark`,
contentDigest,
parent: node.id,
type: `MarkdownRemark`,
mediaType: `text/x-markdown`,
children: [],
content: data.content,
parent: node.id,
internal: {
contentDigest,
type: `MarkdownRemark`,
mediaType: `text/x-markdown`,
content: data.content,
},
}
markdownNode.frontmatter = {
title: ``, // always include a title
Expand All @@ -44,7 +46,7 @@ module.exports = async function onNodeCreate({
}

// Add path to the markdown file path
if (node.type === `File`) {
if (node.internal.type === `File`) {
markdownNode.fileAbsolutePath = node.absolutePath
}

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-transformer-sharp/src/extend-node-type.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Promise = require(`bluebird`)
const {
GraphQLObjectType,
GraphQLBoolean,
Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby-transformer-sharp/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ module.exports = async function onNodeCreate({ node, boundActionCreators }) {
}

const imageNode = {
logical: true,
id: `${node.id} >> ImageSharp`,
contentDigest: `${node.id}`,
parent: node.id,
type: `ImageSharp`,
mediaType: node.mediaType,
children: [],
parent: node.id,
internal: {
contentDigest: `${node.id}`,
type: `ImageSharp`,
mediaType: node.internal.mediaType,
},
}

node.children = node.children.concat([imageNode.id])
Expand Down
17 changes: 11 additions & 6 deletions packages/gatsby-transformer-yaml/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const crypto = require(`crypto`)

async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
const { createNode, updateNode } = boundActionCreators
if (node.mediaType !== `text/yaml`) {
if (node.internal.mediaType !== `text/yaml`) {
return
}

Expand All @@ -19,12 +19,17 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
return {
...obj,
id: obj.id ? obj.id : `${node.id} [${i}] >>> YAML`,
contentDigest,
type: _.upperFirst(_.camelCase(`${node.name} Yaml`)),
mediaType: `application/json`,
parent: node.id,
children: [],
content: objStr,
parent: node.id,
internal: {
contentDigest,
// TODO make choosing the "type" a lot smarter. This assumes
// the parent node is a file.
// PascalCase
type: _.upperFirst(_.camelCase(`${node.name} Yaml`)),
mediaType: `application/json`,
content: objStr,
},
}
})

Expand Down
12 changes: 7 additions & 5 deletions packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export const pageSchema = Joi.object()
export const nodeSchema = Joi.object()
.keys({
id: Joi.string().required(),
contentDigest: Joi.string().required(),
children: Joi.array(Joi.string()).required(),
parent: Joi.string().required(),
mediaType: Joi.string().required(),
content: Joi.string(),
type: Joi.string().required(),
pluginName: Joi.string().required(),
internal: Joi.object().keys({
contentDigest: Joi.string().required(),
mediaType: Joi.string().required(),
type: Joi.string().required(),
pluginName: Joi.string().required(),
content: Joi.string(),
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's added automatically by api-runner-node

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not required according to this validation, shouldn't contentDigest then not be required too? Or are those not added in the same way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jquense to expand on my answer I typed on my phone as my plane took off :-) If we know which plugin created every node we can do optimizations eventually like invalidating only nodes created by a plugin when it updates, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x80 what's "it" here?

})
.unknown()
4 changes: 2 additions & 2 deletions packages/gatsby/src/query-runner/page-query-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ const findAndRunQueriesForDirtyPaths = actions => {
dirtyPaths = dirtyPaths.concat(state.pageDataDependencies.nodes[node.id])
}
// Find invalid connections
if (state.pageDataDependencies.connections[node.type]) {
if (state.pageDataDependencies.connections[node.internal.type]) {
dirtyPaths = dirtyPaths.concat(
state.pageDataDependencies.connections[node.type]
state.pageDataDependencies.connections[node.internal.type]
)
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ exports.loadNodeContent = node => {
// Load plugin's loader function
const plugin = store
.getState()
.flattenedPlugins.find(plug => plug.name === node.pluginName)
.flattenedPlugins.find(plug => plug.name === node.internal.pluginName)
const { loadNodeContent } = require(plugin.resolve)
return loadNodeContent(node).then(content => {
// TODO update node's content field here.
Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby/src/redux/plugin-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const apiRunnerNode = require(`../utils/api-runner-node`)
store.subscribe(() => {
const state = store.getState()
// console.log("last action", state.lastAction.type)
if (
state.lastAction.type === `CREATE_NODE` &&
state.lastAction.payload.logical !== true
) {
if (state.lastAction.type === `CREATE_NODE`) {
const node = state.nodes[state.lastAction.payload.id]
apiRunnerNode(`onNodeCreate`, { node })
}
Expand Down
Loading