Skip to content

Commit 3d8f2c6

Browse files
authored
Merge pull request marcaaron#4 from pasdo501/package
Package -> Master
2 parents 4d506ca + a331063 commit 3d8f2c6

File tree

5 files changed

+1770
-74
lines changed

5 files changed

+1770
-74
lines changed

README.md

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# gatsby-source-woocommerce
1+
# 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).**
23

4+
# gatsby-source-woocommerce
35
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.
46

57
## Install
68

7-
`npm install --save gatsby-source-woocommerce`
9+
`npm install --save @pasdo501/gatsby-source-woocommerce`
810

911
## How to Use
1012

1113
```javascript
1214
// In gatsby-config.js
1315
plugins:[
1416
{
15-
resolve: "gatsby-source-woocommerce",
17+
resolve: '@pasdo501/gatsby-source-woocommerce',
1618
options: {
1719
// Base URL of Wordpress site
1820
api: 'wordpress.domain',
@@ -36,6 +38,7 @@ plugins:[
3638

3739
## Currently Supported Fields
3840

41+
Definitive:
3942
- Products
4043
- Customers
4144
- Orders
@@ -45,3 +48,79 @@ plugins:[
4548
**Note**: If following the endpoint layout from the [WooCommerce REST API docs](https://woocommerce.github.io/woocommerce-rest-api-docs/?php#introduction), all fields that do not contain a wildcard *should* be supported.
4649

4750
For example, to get product categories: including 'products/categories' in fields will show up as allWcProductsCategories / wcProductsCategories
51+
52+
## Some GraphQL Query Examples
53+
54+
### All products (with associated images):
55+
```
56+
{
57+
allWcProducts {
58+
edges {
59+
node {
60+
id
61+
wordpress_id
62+
name
63+
images {
64+
localFile {
65+
// childImageSharp ... etc
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
```
73+
74+
### All product categories (with associated image):
75+
```
76+
{
77+
allWcProductsCategories {
78+
edges {
79+
node {
80+
id
81+
wordpress_id
82+
name
83+
slug
84+
image {
85+
localFile {
86+
// childImageSharp ... etc
87+
}
88+
}
89+
}
90+
}
91+
}
92+
}
93+
```
94+
95+
### Specific product by wordpress ID:
96+
```
97+
{
98+
wcProducts(wordpress_id: {eq: 12}) {
99+
name
100+
price
101+
related_ids
102+
}
103+
}
104+
```
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+
```
123+
124+
## Changelog
125+
- 0.3.2: Mapping products & categories to each other
126+
- 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: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,78 @@
1-
const WooCommerceAPI = require('woocommerce-api');
2-
const { processNode, normaliseFieldName } = require('./helpers');
1+
const WooCommerceAPI = require("woocommerce-api")
2+
const {
3+
processNode,
4+
normaliseFieldName,
5+
mapMediaToNodes,
6+
mapProductsToCategories,
7+
} = require("./helpers")
38

49
exports.sourceNodes = async (
5-
{ boundActionCreators, createNodeId },
10+
{ actions, createNodeId, createContentDigest, store, cache },
611
configOptions
712
) => {
8-
const { createNode } = boundActionCreators;
9-
delete configOptions.plugins;
13+
const { createNode, touchNode } = actions
14+
delete configOptions.plugins
1015

1116
const { api, https, api_keys, fields, api_version = 'wc/v1', per_page } = configOptions;
1217

1318
// set up WooCommerce node api tool
1419
const WooCommerce = new WooCommerceAPI({
15-
url: `http${https?'s':''}://${api}`,
20+
url: `http${https ? "s" : ""}://${api}`,
1621
consumerKey: api_keys.consumer_key,
1722
consumerSecret: api_keys.consumer_secret,
1823
wpAPI: true,
1924
version: api_version
2025
});
2126

2227
// Fetch Node and turn our response to JSON
23-
const fetchNodes = async (fieldName) => {
24-
const endpoint = per_page
25-
? fieldName + `?per_page=${per_page}`
26-
: fieldName;
27-
28-
const res = await WooCommerce.getAsync(endpoint);
29-
const json = res.toJSON();
30-
if(json.statusCode !== 200) {
28+
const fetchNodes = async fieldName => {
29+
const endpoint = per_page ? fieldName + `?per_page=${per_page}` : fieldName
30+
31+
const res = await WooCommerce.getAsync(endpoint)
32+
const json = res.toJSON()
33+
if (json.statusCode !== 200) {
3134
console.warn(`
32-
\n========== WARNING FOR FIELD ${fieldName} ==========\n`
33-
);
34-
console.warn(`The following error message was produced: ${json.body}`);
35-
console.warn(`\n========== END WARNING ==========\n`);
36-
return [];
35+
\n========== WARNING FOR FIELD ${fieldName} ==========\n`)
36+
console.warn(`The following error message was produced: ${json.body}`)
37+
console.warn(`\n========== END WARNING ==========\n`)
38+
return []
3739
}
38-
return JSON.parse(json.body);
39-
};
40+
return JSON.parse(json.body)
41+
}
4042

4143
// Loop over each field set in configOptions and process/create nodes
42-
async function fetchNodesAndCreate (array) {
44+
async function fetchNodesAndCreate(array) {
45+
let nodes = []
4346
for (const field of array) {
44-
const nodes = await fetchNodes(field);
45-
const fieldName = normaliseFieldName(field);
46-
nodes.forEach(n=>createNode(processNode(createNodeId, n, fieldName)));
47+
const fieldName = normaliseFieldName(field)
48+
let tempNodes = await fetchNodes(field)
49+
tempNodes = tempNodes.map(node => {
50+
return {
51+
fieldName,
52+
...node,
53+
}
54+
})
55+
nodes = nodes.concat(tempNodes)
4756
}
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))
4873
}
49-
74+
5075
// Leh go...
51-
await fetchNodesAndCreate(fields);
52-
return;
53-
};
76+
await fetchNodesAndCreate(fields)
77+
return
78+
}

0 commit comments

Comments
 (0)