-
Preliminary Checks
DescriptionIn my application I'm processing remote images using The problem is that when running Gatsby with the nodes (supposedly) cached, the child files are not set on the
I'm able to reproduce this issue with a simple Yaml file and attaching it to the Yaml node as well, so it doesn't seem to be related to the Ghost CMS plugin. See minimal reproduction repo for an example. From my understanding Gatsby only runs Reproduction Linkhttps://github.com/Mrtenz/gatsby-remote-file-node-issue Steps to Reproduce
Expected ResultThe remote file node should be attached to the other node, and the application should run fine. Actual ResultThe remote file node is not attached to the other node, resulting in:
EnvironmentSystem:
OS: Linux 5.14 Arch Linux
CPU: (16) x64 AMD Ryzen 7 3800X 8-Core Processor
Shell: 5.8 - /usr/bin/zsh
Binaries:
Node: 14.18.1 - /tmp/yarn--1635866615094-0.2457912608307613/node
Yarn: 1.22.17 - /tmp/yarn--1635866615094-0.2457912608307613/yarn
npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm
Languages:
Python: 3.9.7 - /usr/bin/python
Browsers:
Firefox: 93.0
npmPackages:
gatsby: ^4.0.2 => 4.0.2
gatsby-source-filesystem: ^4.0.0 => 4.0.0
gatsby-transformer-yaml: ^4.0.0 => 4.0.0 Config FlagsNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi! You're running into two things:
You can fix it like this: // gatsby-node.js
const { createRemoteFileNode } = require('gatsby-source-filesystem');
// Random cute picture
const FILE_URL = 'https://cdn.shibe.online/shibes/530e562bdd24954292fb3ce25aeb2d6180fb5d6e.jpg';
module.exports.onCreateNode = async ({ node, actions, store, cache, reporter, createNodeId }) => {
const { createNode, createNodeField } = actions;
// As an example, attach remote file node to `DataYaml`
if (node.internal.type === 'DataYaml') {
const fileNode = await createRemoteFileNode({
url: FILE_URL,
parentNodeId: node.id,
store,
cache,
reporter,
createNode,
createNodeId
});
if (fileNode) {
createNodeField({ node, name: 'localFile', value: fileNode.id })
}
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(`
type DataYaml implements Node {
childFile: File @link(from: "fields.localFile")
}
`)
} This is explained here: https://www.gatsbyjs.com/docs/how-to/plugins-and-themes/creating-a-source-plugin/#optimize-remote-images |
Beta Was this translation helpful? Give feedback.
-
@LekoArts Maybe I should open a separate thread, but it is a similar question. How would you add a parent/child link in this setup, when using the buildObjectType api? // Add a "fields.localImages" field to Shopify Product integration fields. It
// contains a list of File nodes that lets `gatsby-transformer-sharp` process
// the images.
exports.onCreateNode = async ({
node,
actions: { createNode, createNodeField },
createNodeId,
reporter,
getCache,
store,
}) => {
// Look for Shopify Product integration nodes.
if (
node.internal.type ===
"PrismicHomepageDataBodySmallProductGridItemsProductgridMax3IntegrationType"
) {
const images = node.images
console.log(images)
// For each image, use createRemoteFileNode to download the image and save
// in a new node.
const imageNodes = await Promise.all(
images.map(async (image) => {
if (image.src) {
return await createRemoteFileNode({
url: image.src,
parentNodeId: node.id,
createNode,
createNodeId,
reporter,
getCache,
store,
})
}
})
)
// We will reference each downloaded image node by ID.
const imageNodeIds = imageNodes.map((node) => node.id)
// Add a field called "fields.localImages" to the Shopify Product
// integration node.
createNodeField({
node,
name: "localImages",
value: imageNodeIds,
})
}
}
// Tell Gatsby that `fields.localImages` is a list of File nodes.
exports.createSchemaCustomization = ({ actions, schema }) => {
const shopifyProductType = schema.buildObjectType({
name: "PrismicHomepageDataBodySmallProductGridItemsProductgridMax3IntegrationTypeFields",
fields: {
localImages: {
type: "[File]",
extensions: { link: {} },
},
},
})
actions.createTypes(shopifyProductType) |
Beta Was this translation helpful? Give feedback.
Hi!
You're running into two things:
createParentChildLink
is intended for another use case. It's purpose is to create derived nodes from a parent and not referencing related nodes. It's used in transformers primarily: https://www.gatsbyjs.com/docs/how-to/plugins-and-themes/creating-a-transformer-plugin/#creating-the-transformer-relationshiponCreateNode
the direct mutation of the node won't no longer work with v4: https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v3-to-v4/#dont-mutate-nodes-outside-of-expected-apis - so you're running into that changeYou can fix it like this: