Skip to content

Commit

Permalink
fix: create node field for assets (#224)
Browse files Browse the repository at this point in the history
* fix: use createNodeField for remote node

* chore: update demo to use downloadLocalImages

* fix: set File field link
  • Loading branch information
notrab authored Jan 25, 2022
1 parent 8c6dfa0 commit 297b99e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions demo/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 23 additions & 5 deletions gatsby-source-graphcms/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
`)

Expand All @@ -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,
Expand All @@ -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({
Expand All @@ -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)
}
Expand Down Expand Up @@ -439,7 +449,7 @@ function makeResolveGatsbyImageData(cache) {

export function createResolvers(
{ createResolvers, cache },
{ typePrefix = 'GraphCMS_' }
{ typePrefix = 'GraphCMS_', downloadLocalImages = false }
) {
const args = {
quality: {
Expand All @@ -464,6 +474,14 @@ export function createResolvers(
type: 'JSON',
},
},
...(downloadLocalImages && {
File: {
gatsbyImageData: {
...getGatsbyImageResolver(makeResolveGatsbyImageData(cache), args),
type: 'JSON',
},
},
}),
}

createResolvers(resolvers)
Expand Down

0 comments on commit 297b99e

Please sign in to comment.