-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
42 lines (39 loc) · 923 Bytes
/
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
require('dotenv').config()
const path = require('path')
const slash = require('slash')
const _ = require('lodash')
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
{
wordpress {
posts(where: { status: PUBLISH }) {
edges {
node {
slug
}
}
}
}
}
`)
// Handle errors
if (result.errors) {
console.error(result.errors)
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// Blog Posts
const postTemplate = path.resolve(`./src/templates/blog-post.js`)
_.each(result.data.wordpress.posts.edges, edge => {
const { node } = edge
const post = node
createPage({
path: `/blog/${post.slug}`,
component: slash(postTemplate),
context: {
slug: post.slug,
},
})
})
}