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

Commit

Permalink
feat: connect category and products (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
notrab authored Jul 9, 2020
1 parent dff479b commit 8472c72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions gatsby-source-chec/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exports.sourceNodes = require('./gatsby/node/sourceNodes');
exports.createSchemaCustomization = require('./gatsby/node/createSchemaCustomization');
13 changes: 13 additions & 0 deletions gatsby-source-chec/gatsby/node/createSchemaCustomization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type ChecProduct implements Node {
categories: [ChecCategory] @link
}
type ChecCategory implements Node {
products: [ChecProduct] @link
}
`);
};

module.exports = createSchemaCustomization;
22 changes: 18 additions & 4 deletions gatsby-source-chec/gatsby/node/sourceNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,34 @@ const sourceNodes = async (
},
});

categories.forEach((category) =>
categories.forEach((category) => {
const productIds = products.reduce((ids, product) => {
const matchingCategory = product.categories.find(
(cat) => cat.id === category.id
);

if (!matchingCategory) return ids;

return [...ids, product.id];
}, []);

createNode({
...category,
products: productIds,
internal: {
type: `ChecCategory`,
content: JSON.stringify(category),
contentDigest: createContentDigest(category),
},
})
);
});
});

products.forEach(({ categories, ...product }) => {
const categoryIds = categories.map(({ id }) => id);

products.forEach((product) => {
createNode({
...product,
categories: categoryIds,
internal: {
type: `ChecProduct`,
content: JSON.stringify(product),
Expand Down

0 comments on commit 8472c72

Please sign in to comment.