-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
52 lines (47 loc) · 1.3 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
// graphql function returns a promise so we can use this little promise helper to have a nice result/error state
const wrapper = promise =>
promise.then(result => {
if (result.errors) {
throw result.errors
}
return result
})
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const projectPage = require.resolve('./src/templates/project.js')
const result = await wrapper(
graphql(`
{
prismic {
projects: allProjects(sortBy: completed_DESC) {
edges {
node {
_meta {
uid
}
}
}
}
}
}
`)
)
const projects = result.data.prismic.projects.edges
const length = projects.length
projects.forEach(({ node }, index) => {
const prev =
index === 0 ? projects[length - 1].node : projects[index - 1].node
const next =
index === length - 1 ? projects[0].node : projects[index + 1].node
createPage({
path: node._meta.uid,
component: projectPage,
context: {
// Pass "slug" through context so we can reference it in our query like "$slug: String!"
uid: node._meta.uid,
prevNode: prev._meta.uid,
nextNode: next._meta.uid,
},
})
})
}