Created
April 20, 2019 21:10
-
-
Save spencersmb/ba43646646c83df6e4b5f38985d20ad9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const utils = require("./utils") | |
const fetch = require("node-fetch") | |
const queryString = require("query-string") | |
const fs = require("fs-extra") | |
const { createRemoteFileNode } = require(`gatsby-source-filesystem`) | |
exports.sourceNodes = async ( | |
{ | |
actions, createNodeId, createContentDigest, store, cache | |
}, | |
configOptions | |
) => { | |
const { createNode } = actions | |
await fs.removeSync("/.cache") | |
// Gatsby adds a configOption that's not needed for this plugin, delete it | |
delete configOptions.plugins | |
// Helper function that processes a product to match Gatsby's node structure | |
const processProduct = async (product, args) => { | |
// console.log("product", product) | |
// https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem#createremotefilenode | |
// download and add image to local file | |
await product.images.map(async image => { | |
const fileNode = await createRemoteFileNode({ | |
...args, | |
url: image.fullSize.url | |
}) | |
image.localFile___NODE = fileNode.id | |
}) | |
const nodeId = createNodeId(`wc-product-${product.id}`) | |
const nodeContent = JSON.stringify(product) | |
// Node info | |
return Object.assign({}, product, { | |
id: nodeId, | |
parent: null, | |
children: [], | |
internal: { | |
type: `wcProduct`, | |
content: nodeContent, | |
contentDigest: createContentDigest(product) | |
} | |
}) | |
} | |
// const apiOptions = queryString.stringify(configOptions) | |
// console.log('apiOptions', apiOptions) | |
const dev = process.env.NODE_ENV !== "production" | |
const apiUrl = `${process.env.GATSBY_DB}/wp-json/et-shop/graphql/products` | |
const apiResponse = await fetch(apiUrl) | |
const results = await apiResponse.json() | |
const jsonResults = JSON.stringify(utils.transformNormalizedData(results.data)) | |
fs.writeFileSync("src/state/products.json", jsonResults) | |
results.data.forEach(async (product) => { | |
// Process the product data to match the structure of a Gatsby node | |
const productNode = await processProduct(product, { store, cache, createNode, createNodeId }) | |
// Use Gatsby's createNode helper to create a node from the node data | |
createNode(productNode) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment