-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgatsby-node.ts
55 lines (50 loc) · 1.65 KB
/
gatsby-node.ts
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
import path from "path";
import { CreatePagesArgs } from "gatsby";
import { resPostType, postType, resCollectionType, collectionType } from "./src/types";
const turnProductsIntoPages = async (args: CreatePagesArgs) => {
const { actions, graphql } = args;
const postTemplate = path.resolve("./src/templates/product.tsx");
const res: resPostType = await graphql(`
query createShopifyProduct {
products: allShopifyProduct {
nodes {
handle
id
productType
}
}
}
`);
res.data?.products.nodes.forEach(({ id, productType, handle }: postType) => {
actions.createPage({
path: `collections/${productType}/${handle}/`,
component: postTemplate,
context: { id, location: `/collections/${productType}/${handle}/` },
});
});
};
const turnCollectionsIntoPages = async (args: CreatePagesArgs) => {
const { actions, graphql } = args;
const collectionTemplate = path.resolve("./src/templates/collection.tsx");
const res: resCollectionType = await graphql(`
query createShopifyCollection {
collections: allShopifyCollection {
nodes {
id
title
handle
}
}
}
`);
res.data?.collections.nodes.forEach(({ id, handle, title }: collectionType) => {
actions.createPage({
path: `collections/${handle}/`,
component: collectionTemplate,
context: { id, title, location: `/collections/${handle}/` },
});
});
};
exports.createPages = async (params: CreatePagesArgs) => {
await Promise.all([turnProductsIntoPages(params), turnCollectionsIntoPages(params)]);
};