-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
90 lines (73 loc) · 2.37 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.sourceNodes = void 0;
var _chalk = _interopRequireDefault(require("chalk"));
var _pIteration = require("p-iteration");
var _lib = require("./lib");
var _nodes = require("./nodes");
var _queries = require("./queries");
const sourceNodes = async ({
actions: {
createNode,
touchNode
},
createNodeId,
store,
cache
}, {
shopName,
accessToken,
verbose = true
}) => {
const client = (0, _lib.createClient)(shopName, accessToken); // Convenience function to namespace console messages.
const formatMsg = msg => _chalk.default`\n{blue gatsby-source-shopify-admin/${shopName}} ${msg}`;
try {
console.log(formatMsg(`starting to fetch data from Shopify`)); // Arguments used for file node creation.
const imageArgs = {
createNode,
createNodeId,
touchNode,
store,
cache
}; // Arguments used for node creation.
const args = {
client,
createNode,
createNodeId,
formatMsg,
verbose,
imageArgs
}; // Message printed when fetching is complete.
const msg = formatMsg(`finished fetching data from Shopify`);
console.time(msg);
await createNodes(`productVariants`, _queries.PRODUCT_VARIANTS_QUERY, _nodes.ProductVariantNode, args);
await createNodes(`collections`, _queries.COLLECTIONS_QUERY, _nodes.CollectionNode, args);
console.timeEnd(msg);
} catch (e) {
console.error(_chalk.default`\n{red error} an error occured while sourcing data`); // If not a GraphQL request error, let Gatsby print the error.
if (!e.hasOwnProperty(`request`)) throw e;
(0, _lib.printGraphQLError)(e);
}
};
/**
* Fetch and create nodes for the provided endpoint, query, and node factory.
*/
exports.sourceNodes = sourceNodes;
const createNodes = async (endpoint, query, nodeFactory, {
client,
createNode,
formatMsg,
verbose,
imageArgs
}, f = async () => {}) => {
// Message printed when fetching is complete.
const msg = formatMsg(`fetched and processed ${endpoint}`);
if (verbose) console.time(msg);
await (0, _pIteration.forEach)((await (0, _lib.queryAll)(client, [endpoint], query)), async entity => {
const node = await nodeFactory(imageArgs)(entity);
createNode(node);
await f(entity);
});
if (verbose) console.timeEnd(msg);
};