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

Commit

Permalink
feat: build multiple merchant nodes (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
notrab authored Jun 28, 2021
1 parent c721be3 commit 9721eba
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
42 changes: 21 additions & 21 deletions demo/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;

const {
data: { products, categories },
} = await graphql(`
const { data } = await graphql(`
{
products: allChecProduct {
nodes {
Expand All @@ -21,23 +19,25 @@ exports.createPages = async ({ graphql, actions }) => {
}
`);

products.nodes.forEach(({ id, permalink }) =>
createPage({
path: `/products/${permalink}`,
component: require.resolve(`./src/templates/ProductPage.js`),
context: {
id,
},
})
);
data &&
data.products.nodes.forEach(({ id, permalink }) =>
createPage({
path: `/products/${permalink}`,
component: require.resolve(`./src/templates/ProductPage.js`),
context: {
id,
},
})
);

categories.nodes.forEach(({ id, slug }) =>
createPage({
path: `/categories/${slug}`,
component: require.resolve(`./src/templates/CategoryPage.js`),
context: {
id,
},
})
);
data &&
data.categories.nodes.forEach(({ id, slug }) =>
createPage({
path: `/categories/${slug}`,
component: require.resolve(`./src/templates/CategoryPage.js`),
context: {
id,
},
})
);
};
16 changes: 12 additions & 4 deletions demo/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { graphql, Link } from 'gatsby';
import ProductList from '../components/ProductList';

export default function IndexPage({
data: { merchant, products, categories },
data: { merchants, products, categories },
}) {
return (
<React.Fragment>
<h1>{merchant.name}</h1>
<h1>Merchants</h1>

<ul>
{merchants.nodes.map(({ name }) => (
<li key={name}>{name}</li>
))}
</ul>

<h3>Categories</h3>

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

export const pageQuery = graphql`
{
merchant: checMerchant {
name: business_name
merchants: allChecMerchant {
nodes {
name: business_name
}
}
categories: allChecCategory {
Expand Down
32 changes: 22 additions & 10 deletions gatsby-source-chec/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,31 @@ exports.sourceNodes = async (
}
};

const { id: merchantId, ...merchant } = await commerce.get('merchants');
const createMerchant = (merchant) =>
createNode({
...merchant,
id: merchant.id.toString(),
internal: {
type: `ChecMerchant`,
content: JSON.stringify(merchant),
contentDigest: createContentDigest(merchant),
},
});

const merchant = await commerce.get('merchants');

let merchants = [];

if (merchant && merchant.id) {
merchants = [merchant];
} else {
merchants = await fetchAllPages('merchants');
}

const categories = await fetchAllPages('categories');
const products = await fetchAllPages('products');

createNode({
id: merchantId.toString(),
...merchant,
internal: {
type: `ChecMerchant`,
content: JSON.stringify(merchant),
contentDigest: createContentDigest(merchant),
},
});
merchants.forEach(createMerchant);

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

0 comments on commit 9721eba

Please sign in to comment.