Skip to content

Commit

Permalink
Move buildTime into new node type (#21827)
Browse files Browse the repository at this point in the history
* Move buildTime into new node type

* Use original resolver

* Call the type SiteMetadata

* Update snapshot
  • Loading branch information
ascorbic authored Mar 2, 2020
1 parent cc8c90e commit d877b82
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Array [
"name": "internal-data-bridge",
"nodeAPIs": Array [
"sourceNodes",
"createSchemaCustomization",
"createResolvers",
"onCreatePage",
],
"pluginOptions": Object {
Expand Down Expand Up @@ -220,6 +222,8 @@ Array [
"name": "internal-data-bridge",
"nodeAPIs": Array [
"sourceNodes",
"createSchemaCustomization",
"createResolvers",
"onCreatePage",
],
"pluginOptions": Object {
Expand Down Expand Up @@ -432,6 +436,8 @@ Array [
"name": "internal-data-bridge",
"nodeAPIs": Array [
"sourceNodes",
"createSchemaCustomization",
"createResolvers",
"onCreatePage",
],
"pluginOptions": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ exports.sourceNodes = ({ createContentDigest, actions, store }) => {
})

// Add site node.
const buildTime = moment()
.subtract(process.uptime(), `seconds`)
.toJSON()

const createGatsbyConfigNode = (config = {}) => {
// Delete plugins from the config as we add plugins above.
Expand All @@ -93,7 +90,6 @@ exports.sourceNodes = ({ createContentDigest, actions, store }) => {
port: state.program.port,
host: state.program.host,
...configCopy,
buildTime,
}
createNode({
...node,
Expand All @@ -109,6 +105,24 @@ exports.sourceNodes = ({ createContentDigest, actions, store }) => {

createGatsbyConfigNode(state.config)

const buildTime = moment()
.subtract(process.uptime(), `seconds`)
.startOf(`second`)
.toJSON()

const metadataNode = { buildTime }

createNode({
...metadataNode,
id: `SiteBuildMetadata`,
parent: null,
children: [],
internal: {
contentDigest: createContentDigest(metadataNode),
type: `SiteBuildMetadata`,
},
})

const pathToGatsbyConfig = systemPath.join(
program.directory,
`gatsby-config.js`
Expand All @@ -129,6 +143,42 @@ exports.sourceNodes = ({ createContentDigest, actions, store }) => {
})
}

exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type Site implements Node {
buildTime: Date @dateformat
}
`
createTypes(typeDefs)
}

exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
Site: {
buildTime: {
type: `Date`,
resolve(source, args, context, info) {
const { buildTime } = context.nodeModel.getNodeById({
id: `SiteBuildMetadata`,
type: `SiteBuildMetadata`,
})
return info.originalResolver(
{
...source,
buildTime,
},
args,
context,
info
)
},
},
},
}
createResolvers(resolvers)
}

exports.onCreatePage = ({ createContentDigest, page, actions }) => {
const { createNode } = actions
// eslint-disable-next-line
Expand Down

0 comments on commit d877b82

Please sign in to comment.