-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
114 lines (106 loc) · 2.42 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
110
111
112
113
114
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const makeRequest = (graphql, request) => new Promise((resolve, reject) =>
resolve(
graphql(request).then(result => {
if (result.errors) {
reject(result.errors)
}
return result;
})
))
// Implement the Gatsby API “createPages”. This is called once the
// data layer is bootstrapped to let plugins create pages from data.
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const getArticles = makeRequest(graphql, `
{
allWordpressPost {
edges {
node {
categories { name }
slug
author { name }
}
}
}
}
`).then(result =>
result.data.allWordpressPost.edges.forEach(({ node }) =>
createPage({
path: `/${ node.categories[0].name }/${ node.slug }`,
component: path.resolve(`src/templates/article.tsx`),
context: {
slug: node.slug,
},
})
)
)
const getCategories = makeRequest(graphql, `
{
allWordpressCategory {
edges {
node { name }
}
}
}
`).then(result =>
result.data.allWordpressCategory.edges.forEach(({ node }) =>
createPage({
path: `/${ node.name }`,
component: path.resolve(`src/templates/category.tsx`),
context: {
category: node.name,
},
})
)
)
const getAuthors = makeRequest(graphql, `
{
allWordpressWpUsers {
edges {
node { slug }
}
}
}
`).then(result =>
result.data.allWordpressWpUsers.edges.forEach(({ node }) =>
createPage({
path: `/authors/${ node.slug }`,
component: path.resolve(`src/templates/author.tsx`),
context: {
username: node.slug,
},
})
)
)
const getTags = makeRequest(graphql, `
{
allWordpressTag {
edges {
node { name }
}
}
}
`).then(result =>
result.data.allWordpressTag.edges.forEach(({ node }) =>
createPage({
path: `/tags/${ node.name }`,
component: path.resolve(`src/templates/tag.tsx`),
context: {
tag: node.name,
},
})
)
)
return Promise.all([
getArticles,
getCategories,
getAuthors,
getTags,
])
}