Skip to content

Commit 6df73c3

Browse files
committed
0.3.2 - Map Products to Categories
- Now mapping products to categories (& vice versa) - Changed the way nodes are created to allow for that - first fetch all nodes, then process them in bulk rather than field by field - Updated README - Updated package.json
1 parent 22f25a0 commit 6df73c3

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Note
2-
**Please note: the original author of this package is [Marc Glasser](https://github.com/marcaaron/) -- see the [the original repo](https://github.com/marcaaron/gatsby-source-woocommerce) and [package](https://www.npmjs.com/package/gatsby-source-woocommerce). This is a modified version for my use to avoid having to manually overwrite the files every time I change my packages in development.**
2+
**Please note: the original author of this package is [Marc Glasser](https://github.com/marcaaron/) -- see the [the original repo](https://github.com/marcaaron/gatsby-source-woocommerce) and [package](https://www.npmjs.com/package/gatsby-source-woocommerce).**
33

44
# gatsby-source-woocommerce
55
Source plugin for [Gatsby](https://www.gatsbyjs.org/). Pulls in data from protected routes via the [WooCommerce REST API](http://woocommerce.github.io/woocommerce-rest-api-docs/) with credentials.
@@ -51,7 +51,7 @@ For example, to get product categories: including 'products/categories' in field
5151

5252
## Some GraphQL Query Examples
5353

54-
All products (with associated images):
54+
### All products (with associated images):
5555
```
5656
{
5757
allWcProducts {
@@ -71,7 +71,7 @@ All products (with associated images):
7171
}
7272
```
7373

74-
All product categories (with associated image):
74+
### All product categories (with associated image):
7575
```
7676
{
7777
allWcProductsCategories {
@@ -92,7 +92,7 @@ All product categories (with associated image):
9292
}
9393
```
9494

95-
Specific product by wordpress ID:
95+
### Specific product by wordpress ID:
9696
```
9797
{
9898
wcProducts(wordpress_id: {eq: 12}) {
@@ -102,7 +102,25 @@ Specific product by wordpress ID:
102102
}
103103
}
104104
```
105+
### Specific product category with associated products
106+
```
107+
{
108+
wcProductsCategories(wordpress_id: {eq: 20}) {
109+
name
110+
slug
111+
products {
112+
name
113+
price
114+
images {
115+
localFile {
116+
// childImageSharp ... etc
117+
}
118+
}
119+
}
120+
}
121+
}
122+
```
105123

106124
## Changelog
107-
- 0.3.2: Minor refactor, no notable changes in functionality
125+
- 0.3.2: Mapping products & categories to each other
108126
- 0.3.0: Associated products & product categories with local file images downloaded during the build process to allow use of image transform plugins.

gatsby-node.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
processNode,
44
normaliseFieldName,
55
mapMediaToNodes,
6+
mapProductsToCategories,
67
} = require("./helpers")
78

89
exports.sourceNodes = async (
@@ -41,22 +42,34 @@ exports.sourceNodes = async (
4142

4243
// Loop over each field set in configOptions and process/create nodes
4344
async function fetchNodesAndCreate(array) {
45+
let nodes = []
4446
for (const field of array) {
45-
let nodes = await fetchNodes(field)
4647
const fieldName = normaliseFieldName(field)
47-
nodes = await mapMediaToNodes({
48-
nodes,
49-
store,
50-
cache,
51-
createNode,
52-
createNodeId,
53-
touchNode,
48+
let tempNodes = await fetchNodes(field)
49+
tempNodes = tempNodes.map(node => {
50+
return {
51+
fieldName,
52+
...node,
53+
}
5454
})
55-
56-
nodes.forEach(n =>
57-
createNode(processNode(createNodeId, createContentDigest, n, fieldName))
58-
)
55+
nodes = nodes.concat(tempNodes)
5956
}
57+
58+
nodes = await mapMediaToNodes({
59+
nodes,
60+
store,
61+
cache,
62+
createNode,
63+
createNodeId,
64+
touchNode,
65+
})
66+
67+
nodes = nodes.map(node =>
68+
processNode(createNodeId, createContentDigest, node)
69+
)
70+
nodes = mapProductsToCategories(nodes)
71+
72+
nodes.forEach(node => createNode(node))
6073
}
6174

6275
// Leh go...

helpers/index.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
22

3-
const processNode = (createNodeId, createContentDigest, node, fieldName) => {
3+
const processNode = (createNodeId, createContentDigest, node) => {
4+
const { fieldName } = node
5+
delete node.fieldName
46
const nodeContent = JSON.stringify(node)
57

68
const nodeData = Object.assign({}, node, {
@@ -18,6 +20,37 @@ const processNode = (createNodeId, createContentDigest, node, fieldName) => {
1820
return nodeData
1921
}
2022

23+
// Create links between products and categories (bi-directional)
24+
const mapProductsToCategories = nodes => {
25+
const categories = nodes.filter(
26+
node => node.internal.type === "wcProductsCategories"
27+
)
28+
29+
return nodes.map(node => {
30+
if (categories.length && node.internal.type === "wcProducts") {
31+
node.categories.forEach(({ id }) => {
32+
const category = categories.find(c => id === c.wordpress_id)
33+
if (category) {
34+
if (!node.categories_conection___NODE) {
35+
// Initialise the connection array if necessary
36+
node.categories_connection___NODE = []
37+
}
38+
// Add the current category ID to the connection array
39+
node.categories_connection___NODE.push(category.id)
40+
41+
if (!category.products___NODE) {
42+
// Initialise the product connection array if necessary
43+
category.products___NODE = []
44+
}
45+
// Add the current product's ID to the connection array
46+
category.products___NODE.push(node.id)
47+
}
48+
})
49+
}
50+
return node
51+
})
52+
}
53+
2154
// Turn multi part endpoints into camelCase
2255
// e.g. products/categories becomes productsCategories
2356
const normaliseFieldName = name => {
@@ -45,7 +78,7 @@ const downloadMedia = async ({
4578

4679
if (cacheMediaData && n.modified === cacheMediaData.modified) {
4780
fileNodeID = cacheMediaData.fileNodeID
48-
touchNode({ nodeId: cacheMediaData.fileNodeID })
81+
touchNode({ nodeId: fileNodeID })
4982
}
5083

5184
if (!fileNodeID) {
@@ -118,7 +151,12 @@ const mapMediaToNodes = async ({
118151
)
119152
}
120153

121-
module.exports = { processNode, normaliseFieldName, mapMediaToNodes }
154+
module.exports = {
155+
processNode,
156+
normaliseFieldName,
157+
mapMediaToNodes,
158+
mapProductsToCategories,
159+
}
122160

123161
// Helper Helpers
124162
function capitalize(s) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@pasdo501/gatsby-source-woocommerce",
33
"version": "0.3.2",
4-
"description": "Modified gatsby source plugin for WooCommerce with improved field type support, scoped to avoid having to manually overwrite changes while pull request is open (or forever)",
4+
"description": "Gatsby source plugin for WooCommerce with improved field type support",
55
"main": "gatsby-node.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9-
"author": "Marc Aaron Glasser",
9+
"author": "Dominik Paschke",
1010
"license": "ISC",
1111
"dependencies": {
1212
"gatsby-source-filesystem": "^2.0.27",

0 commit comments

Comments
 (0)