-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
42 lines (36 loc) · 852 Bytes
/
gatsby-node.js
File metadata and controls
42 lines (36 loc) · 852 Bytes
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
const path = require(`path`)
exports.createPages = async ({ graphql, actions, reporter }) => {
const postTemplate = path.resolve(`./src/layouts/postLayout.tsx`)
// Query Ghost data
const result = await graphql(`
{
allGhostPost(sort: { order: ASC, fields: published_at }) {
edges {
node {
slug
}
}
}
}
`)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
if (!result.data.allGhostPost) {
return
}
// Create pages for each Ghost post
const items = result.data.allGhostPost.edges
items.forEach(({ node }) => {
node.url = `/blog/${node.slug}/`
actions.createPage({
path: node.url,
component: postTemplate,
context: {
slug: node.slug
}
})
})
}