diff --git a/demo/gatsby-config.js b/demo/gatsby-config.js index 3b440d3..f321ec8 100644 --- a/demo/gatsby-config.js +++ b/demo/gatsby-config.js @@ -10,6 +10,7 @@ module.exports = { resolve: 'gatsby-source-graphcms', options: { buildMarkdownNodes: true, + downloadLocalImages: true, endpoint: process.env.GRAPHCMS_ENDPOINT || 'https://api-eu-central-1.graphcms.com/v2/ckclvjtet0f0901z69og3f3gm/master', diff --git a/gatsby-source-graphcms/src/gatsby-node.js b/gatsby-source-graphcms/src/gatsby-node.js index 53b38e4..04bb2a1 100644 --- a/gatsby-source-graphcms/src/gatsby-node.js +++ b/gatsby-source-graphcms/src/gatsby-node.js @@ -281,7 +281,7 @@ export async function createSchemaCustomization(gatsbyApi, pluginOptions) { if (downloadLocalImages) createTypes(` type ${typePrefix}Asset { - localFile: File @link + localFile: File @link(from: "fields.localFile") } `) @@ -304,7 +304,13 @@ export async function createSchemaCustomization(gatsbyApi, pluginOptions) { } export async function onCreateNode( - { node, actions: { createNode }, createNodeId, getCache, cache }, + { + node, + actions: { createNode, createNodeField }, + createNodeId, + getCache, + cache, + }, { buildMarkdownNodes = false, downloadLocalImages = false, @@ -314,7 +320,9 @@ export async function onCreateNode( if ( downloadLocalImages && node.remoteTypeName === 'Asset' && - node.mimeType.includes('image/') + ['image/png', 'image/jpg', 'image/jpeg', 'image/tiff'].includes( + node.mimeType + ) ) { try { const fileNode = await createRemoteFileNode({ @@ -327,7 +335,9 @@ export async function onCreateNode( ...(node.fileName && { name: node.fileName }), }) - if (fileNode) node.localFile = fileNode.id + if (fileNode) { + createNodeField({ node, name: 'localFile', value: fileNode.id }) + } } catch (e) { console.error(`[${PLUGIN_NAME}]`, e) } @@ -439,7 +449,7 @@ function makeResolveGatsbyImageData(cache) { export function createResolvers( { createResolvers, cache }, - { typePrefix = 'GraphCMS_' } + { typePrefix = 'GraphCMS_', downloadLocalImages = false } ) { const args = { quality: { @@ -464,6 +474,14 @@ export function createResolvers( type: 'JSON', }, }, + ...(downloadLocalImages && { + File: { + gatsbyImageData: { + ...getGatsbyImageResolver(makeResolveGatsbyImageData(cache), args), + type: 'JSON', + }, + }, + }), } createResolvers(resolvers)