-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
86 lines (74 loc) · 1.86 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
const path = require(`path`)
const productsPath = "/products"
exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type BuiltOnProduct implements Node {
path: String!
}
`)
}
exports.createResolvers = ({ createResolvers }) => {
createResolvers({
BuiltOnProduct: {
path: {
resolve: source => path.join(productsPath, source.id),
},
},
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// Creates /products/:id
const allBuiltOnProduct = await graphql(`
query {
allBuiltOnProduct {
edges {
node {
id
path
}
}
}
}
`)
const builtonProducts = allBuiltOnProduct.data.allBuiltOnProduct.edges
builtonProducts.forEach(({ node }) => {
createPage({
path: node.path,
component: path.resolve(`./src/templates/product.js`),
context: {
id: node.id,
},
})
})
// Creates /products-paginated/*
const allBuiltOnMainProduct = await graphql(`
query {
allBuiltOnProduct(filter: { main_product: { eq: true } }) {
edges {
node {
id
path
}
}
}
}
`)
const builtonMainProducts = allBuiltOnMainProduct.data.allBuiltOnProduct.edges
const productPerPage = 5
const numPages = Math.ceil(builtonMainProducts.length / productPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
matchPath: i === 0 ? `/products-paginated/*` : undefined,
path: `/products-paginated/${i + 1}`,
component: path.resolve("./src/templates/productsPaginated.js"),
context: {
limit: productPerPage,
skip: i * productPerPage,
numPages,
currentPage: i + 1,
basePath: "/products-paginated/",
},
})
})
}