Since Gatsby 4 doesn't accept remoteFileNodes to be created within the createResolvers API inside gatsby-node.js, i'm looking for another solution to create File's from our remote (graphql) source.
Creating files on the upper level of an object works just fine, however i can't find a way to create these files inside nested objects in my schema.
Although there is a File object created with the provided name inside Sections, all the data inside of it results in null. The given URL is checked and is valid.
The following code is inside my gatsby-node.js file:
sourceNodes
exports.sourceNodes = async ({ actions, createContentDigest, createNodeId }) => {
const { createNode } = actions;
const { data } = await client.query({
query: gql`
query {
PageContent {
id
main_image_url
blocks {
title
sections {
title
nested_image_url
}
}
}
}
`,
});
data.PageContent.forEach((pageContent) => {
createNode({
...pageContent,
id: createNodeId(`${PAGE_CONTENT}-${pageContent.id}`),
parent: null,
children: [],
internal: {
type: PAGE_CONTENT,
content: JSON.stringify(pageContent),
contentDigest: createContentDigest(pageContent),
}
})
});
return;
};
onCreateNode
exports.onCreateNode = async ({
node,
actions: { createNode, createNodeField },
createNodeId,
getCache,
}) => {
if (node.internal.type === PAGE_CONTENT) {
This works just fine
if (node.main_image_url) {
const fileNode = await createRemoteFileNode({
url: node.main_image_url,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
});
if (fileNode) {
createNodeField({ node, name: "main_image", value: fileNode.id });
}
}
But this won't
if (node.blocks && node.blocks.length > 0) {
node.blocks.map(async({ sections }) => {
if (sections.length > 0) {
sections.map(async(section) => {
if (section.nested_image_url) {
const fileNode = await createRemoteFileNode({
url: section.nested_image_url,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
});
if (fileNode) {
createNodeField({ node, name: "nested_image", value: fileNode.id });
}
}
})
}
})
}
}
};
createSchema
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type PageContent implements Node {
main_image: File @link(from: "fields.main_image")
blocks: [Block]
}
type Block {
sections: [Section]
}
type Section {
nested_image: File @link(from: "fields.nested_image")
}
`);
};
Would be really grateful if someone has a clue!