-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
88 lines (82 loc) · 2.15 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
const path = require(`path`);
const { isFuture, parseISO } = require('date-fns');
exports.createSchemaCustomization = ({ actions, schema }) => {
actions.createTypes([
schema.buildObjectType({
name: 'SanityPost',
interfaces: ['Node'],
fields: {
isPublished: {
type: 'Boolean!',
resolve: (source) => new Date(source.publishedAt) <= new Date(),
},
},
}),
]);
};
async function fetchCoffeeAndTurnIntoPages({ graphql, actions }) {
const singleCoffeePage = path.resolve('./src/templates/SingleCoffeePage.js');
const { data } = await graphql(`
query {
coffees: allSanityCoffee(filter: { stock: { gt: 0 } }) {
nodes {
name
slug {
current
}
}
}
}
`);
data.coffees.nodes.forEach((coffee) => {
actions.createPage({
path: `coffee/${coffee.slug.current}`,
component: singleCoffeePage,
context: {
slug: coffee.slug.current,
},
});
});
}
async function createBlogPostPages({ graphql, actions }) {
const blogTemplate = path.resolve('./src/templates/BlogPost.js');
const result = await graphql(`
{
allSanityPost(
filter: { slug: { current: { ne: null } }, isPublished: { eq: true } }
) {
edges {
node {
id
publishedAt
slug {
current
}
}
}
}
}
`);
if (result.errors) throw result.errors;
const postEdges = (result.data.allSanityPost || {}).edges || [];
postEdges
.filter((edge) => !isFuture(parseISO(edge.node.publishedAt)))
.forEach((edge) => {
const { id, slug = {} } = edge.node;
const path = `/blog/${slug.current}/`;
console.log(`Creating blog post page: ${path}`);
actions.createPage({
path,
component: blogTemplate,
context: { id },
});
});
}
exports.createPages = async (params) => {
// Create pages dynamically
// Wait for all promises to be resolved before finishing this function
await Promise.all([
fetchCoffeeAndTurnIntoPages(params),
createBlogPostPages(params),
]);
};