-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
78 lines (68 loc) · 1.75 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
const path = require(`path`);
exports.createPages = async ({ actions, graphql }) => {
const QUERY_PRODUCT_LIST = `
query productList($after: String) {
cms {
products(first: 100 after: $after, where: { stockStatus: IN_STOCK }) {
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
edges {
node {
id
name
slug
}
}
}
}
}
`;
const { data: initialResponse } = await graphql(QUERY_PRODUCT_LIST);
let data = initialResponse;
let productList = [...data.cms.products.edges];
while (data.cms.products.pageInfo.hasNextPage) {
const { data: newResponse } = await graphql(QUERY_PRODUCT_LIST, {
after: data.cms.products.pageInfo.endCursor,
});
data = newResponse;
productList = [...productList, ...data.cms.products.edges];
}
productList.forEach((edge) => {
actions.createPage({
path: edge.node.slug,
component: path.resolve(`./src/templates/product/Product.tsx`),
context: {
id: edge.node.id,
},
});
});
const QUERY_PRODUCT_CATEGORIES = `
query productCategories {
cms {
productCategories {
edges {
node {
id
name
slug
}
}
}
}
}
`;
const { data: productCategories } = await graphql(QUERY_PRODUCT_CATEGORIES);
productCategories.cms.productCategories.edges.forEach((category) => {
actions.createPage({
path: category.node.slug,
component: path.resolve(`./src/templates/category/Category.tsx`),
context: {
id: category.node.id,
},
});
});
};