-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
163 lines (146 loc) · 3.78 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import path from 'path';
import fetch from 'isomorphic-fetch';
const turnPizzasIntoPages = async ({ graphql, actions }) => {
// 1. get template for this page
const pizzaTemplate = path.resolve('./src/templates/Pizza.js');
// 2. query all pizzas
const { data } = await graphql(`
query {
pizzas: allSanityPizza {
nodes {
name
slug {
current
}
}
}
}
`);
// 3. loop over each pizza, create page for each
data.pizzas.nodes.forEach((pizza) => {
actions.createPage({
path: `pizza/${pizza.slug.current}`,
component: pizzaTemplate,
context: {
slug: pizza.slug.current,
},
});
});
};
const turnToppingsIntoPages = async ({ graphql, actions }) => {
// 1. get template for this page
const toppingTemplate = path.resolve('./src/pages/pizzas.js');
// 2. query all toppings
const { data } = await graphql(`
query {
toppings: allSanityTopping {
nodes {
id
name
}
}
}
`);
// 3. loop over each pizza, create page for each
data.toppings.nodes.forEach((topping) => {
actions.createPage({
path: `topping/${topping.name}`,
component: toppingTemplate,
context: {
topping: topping.name,
},
});
});
};
const fetchBeersAndTurnIntoNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
// 1. Fetch a list of beers
const res = await fetch('https://api.sampleapis.com/beers/ale');
if (res.ok) {
console.log('*** retrieved sample beers!');
} else {
console.log('*** FAILED to retrieve sample beers!!');
console.log('res.status:', res.status);
}
const beers = await res.json();
// 2. Loop over each beer
for (const beer of beers) {
const nodeMeta = {
id: createNodeId(`beer-${beer.name}`),
parent: null,
children: [],
internal: {
type: 'Beer',
mediaType: 'application/json',
contentDigest: createContentDigest(beer),
},
};
// 3. Create a node for that beer
actions.createNode({
...beer,
...nodeMeta,
});
}
};
const turnSlicemastersIntoPages = async ({ graphql, actions }) => {
// 1. query all slicemasters
const { data } = await graphql(`
query {
slicemasters: allSanityPerson {
totalCount
nodes {
name
id
slug {
current
}
}
}
}
`);
// TODO 2. turn each slicemaster into their own page
data.slicemasters.nodes.forEach((slicemaster) => {
actions.createPage({
component: path.resolve('./src/templates/Slicemaster.js'),
path: `/slicemaster/${slicemaster.slug.current}`,
context: {
name: slicemaster.name,
slug: slicemaster.slug.current,
},
});
});
// 3. calculate number of pages
const pageSize = parseInt(process.env.GATSBY_PAGE_SIZE);
const pageCount = Math.ceil(data.slicemasters.totalCount / pageSize);
// 4. loop from 1 to n (pages) and create page for each
Array.from({ length: pageCount }).forEach((_, i) => {
actions.createPage({
path: `/slicemasters/${i + 1}`,
component: path.resolve('./src/pages/slicemasters.js'),
// this data is passed to the template
context: {
skip: i * pageSize,
currentPage: i + 1,
pageSize,
},
});
});
};
export async function sourceNodes(params) {
// fetch a list of beers and source them into our gatsby API!
await Promise.all([fetchBeersAndTurnIntoNodes(params)]);
}
export async function createPages(params) {
// create pages dynamically
// 1. pizzas
await Promise.all([
turnPizzasIntoPages(params),
turnToppingsIntoPages(params),
turnSlicemastersIntoPages(params),
]);
// 2. toppings
// 3. slicemasters
}