【发布时间】:2019-09-10 17:39:05
【问题描述】:
我正在尝试从 WP 获取一些图像,因为 Gatsby 还不能与 woocommerece 一起使用。我下面的插件能够在构建时转换图像并将它们添加到 .cache 中,但它不会在 Gatsby graphQl 中添加 localFile___NODE 字段。我似乎根本没有添加任何节点来使用 ImageSharp 插件进行查询。 Graphql 显示它们正在 ALLFiles 下进行处理,但不在我在 Graphql 中创建的 wcProduct 节点中……这是怎么回事,这个插件不再创建节点了……
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 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)
})
}
【问题讨论】: