Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit 9721eba

Browse files
authored
feat: build multiple merchant nodes (#56)
1 parent c721be3 commit 9721eba

File tree

3 files changed

+55
-35
lines changed

3 files changed

+55
-35
lines changed

demo/gatsby-node.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
exports.createPages = async ({ graphql, actions }) => {
22
const { createPage } = actions;
33

4-
const {
5-
data: { products, categories },
6-
} = await graphql(`
4+
const { data } = await graphql(`
75
{
86
products: allChecProduct {
97
nodes {
@@ -21,23 +19,25 @@ exports.createPages = async ({ graphql, actions }) => {
2119
}
2220
`);
2321

24-
products.nodes.forEach(({ id, permalink }) =>
25-
createPage({
26-
path: `/products/${permalink}`,
27-
component: require.resolve(`./src/templates/ProductPage.js`),
28-
context: {
29-
id,
30-
},
31-
})
32-
);
22+
data &&
23+
data.products.nodes.forEach(({ id, permalink }) =>
24+
createPage({
25+
path: `/products/${permalink}`,
26+
component: require.resolve(`./src/templates/ProductPage.js`),
27+
context: {
28+
id,
29+
},
30+
})
31+
);
3332

34-
categories.nodes.forEach(({ id, slug }) =>
35-
createPage({
36-
path: `/categories/${slug}`,
37-
component: require.resolve(`./src/templates/CategoryPage.js`),
38-
context: {
39-
id,
40-
},
41-
})
42-
);
33+
data &&
34+
data.categories.nodes.forEach(({ id, slug }) =>
35+
createPage({
36+
path: `/categories/${slug}`,
37+
component: require.resolve(`./src/templates/CategoryPage.js`),
38+
context: {
39+
id,
40+
},
41+
})
42+
);
4343
};

demo/src/pages/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import { graphql, Link } from 'gatsby';
44
import ProductList from '../components/ProductList';
55

66
export default function IndexPage({
7-
data: { merchant, products, categories },
7+
data: { merchants, products, categories },
88
}) {
99
return (
1010
<React.Fragment>
11-
<h1>{merchant.name}</h1>
11+
<h1>Merchants</h1>
12+
13+
<ul>
14+
{merchants.nodes.map(({ name }) => (
15+
<li key={name}>{name}</li>
16+
))}
17+
</ul>
1218

1319
<h3>Categories</h3>
1420

@@ -29,8 +35,10 @@ export default function IndexPage({
2935

3036
export const pageQuery = graphql`
3137
{
32-
merchant: checMerchant {
33-
name: business_name
38+
merchants: allChecMerchant {
39+
nodes {
40+
name: business_name
41+
}
3442
}
3543
3644
categories: allChecCategory {

gatsby-source-chec/gatsby-node.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,31 @@ exports.sourceNodes = async (
5151
}
5252
};
5353

54-
const { id: merchantId, ...merchant } = await commerce.get('merchants');
54+
const createMerchant = (merchant) =>
55+
createNode({
56+
...merchant,
57+
id: merchant.id.toString(),
58+
internal: {
59+
type: `ChecMerchant`,
60+
content: JSON.stringify(merchant),
61+
contentDigest: createContentDigest(merchant),
62+
},
63+
});
64+
65+
const merchant = await commerce.get('merchants');
66+
67+
let merchants = [];
68+
69+
if (merchant && merchant.id) {
70+
merchants = [merchant];
71+
} else {
72+
merchants = await fetchAllPages('merchants');
73+
}
74+
5575
const categories = await fetchAllPages('categories');
5676
const products = await fetchAllPages('products');
5777

58-
createNode({
59-
id: merchantId.toString(),
60-
...merchant,
61-
internal: {
62-
type: `ChecMerchant`,
63-
content: JSON.stringify(merchant),
64-
contentDigest: createContentDigest(merchant),
65-
},
66-
});
78+
merchants.forEach(createMerchant);
6779

6880
categories.forEach((category) => {
6981
const productIds = products.reduce((ids, product) => {

0 commit comments

Comments
 (0)