-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
109 lines (97 loc) · 2.87 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const dbConn = require('./functions/lib/db')
const helpers = require('./src/lib/helpers')
const path = require('path')
// get data from mySQL and put it into GraphQL
// I don't really know why this is considered better than just calling it directly
exports.sourceNodes = async ({ actions: { createNode }, createContentDigest }) => {
let data = await dbConn( async (conn) => {
let rows
if(process.env.LIMIT_ROWS) {
rows = await conn.query("SELECT * from content WHERE draft is FALSE ORDER BY published DESC LIMIT ?",[parseInt(process.env.LIMIT_ROWS)])
} else {
rows = await conn.query("SELECT * from content WHERE draft is FALSE ORDER BY published DESC")
}
return rows
})
// create nodes for each blog post
for(let post of data) {
createNode({
id: post.codename || '__noid',
parent: null,
children: [],
postData: post,
internal: {
type: `BlogPost`,
contentDigest: createContentDigest(data),
},
})
}
}
let getPosts = async (graphql) => {
const { data } = await graphql(`
query BlogPostQuery {
__typename
allBlogPost {
nodes {
postData {
id
title
codename
body
created
draft
excerpt
published
updated
}
}
}
}
`)
let posts = data.allBlogPost.nodes.map( p => {
return p.postData
})
return posts
}
// you could run stuff here
exports.onPreBootstrap = async () => {
}
exports.createPages = async ({ graphql, actions }) => {
let posts = await getPosts(graphql)
// create individual posts
for(let i = 0; i < posts.length; i++) {
let post = posts[i]
console.log(`Creating ${post.codename}`)
actions.createPage({
path: helpers.makeLink(post.codename),
component: path.resolve('./src/components/post-page.js'),
context: {
post
},
})
}
// create giant archive page
actions.createPage({
path: "/archive",
component: path.resolve('./src/components/archive-page.js'),
context: {
posts
},
})
}
// recreates each page node with additional context
// adds recent posts to every page
exports.onCreatePage = async ({ getNodesByType, page, actions }) => {
let posts = getNodesByType("BlogPost").map( p => {
return p.postData
})
const { createPage, deletePage } = actions
deletePage(page)
createPage({
...page,
context: {
...page.context,
recentPosts: posts.slice(0,10)
},
})
}