-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
60 lines (53 loc) · 1.27 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
const path = require("path")
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return graphql(`
{
allBooksJson(limit: 1000) {
edges {
node {
id
songs {
id
}
}
}
}
}
`).then(result => {
if (result.errors) {
throw result.errors
}
const bookTemplate = path.resolve("src/templates/book.tsx")
const songTemplate = path.resolve("src/templates/song.tsx")
result.data.allBooksJson.edges.forEach((edge, bookIndex) => {
const book = edge.node
// Create a book details page for each book
createPage({
path: `/${book.id}`,
component: bookTemplate,
context: {
bookId: book.id,
index: bookIndex,
},
})
// Create a song details page for each song
book.songs.forEach((song, songIndex) => {
createPage({
path: `/${book.id}/${song.id}`,
component: songTemplate,
context: {
songId: song.id,
index: songIndex,
bookId: book.id,
},
})
})
})
})
}