From 4507a7a6867753410cd92730acb12c6abf36461b Mon Sep 17 00:00:00 2001 From: denisgoryaynov Date: Sat, 6 Jul 2019 19:45:03 +0200 Subject: [PATCH 1/5] Added support for nested images --- src/normalize.js | 139 +++++++++++++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 53 deletions(-) diff --git a/src/normalize.js b/src/normalize.js index 0487537..02ce7c2 100644 --- a/src/normalize.js +++ b/src/normalize.js @@ -5,66 +5,97 @@ const extractFields = async ( store, cache, createNode, + createNodeId, touchNode, auth, - item + item, + key = 'localFile' ) => { - for (const key of Object.keys(item)) { - const field = item[key] - if (Array.isArray(field)) { - // add recursion to fetch nested strapi references - await Promise.all( - field.map(async f => - extractFields(apiURL, store, cache, createNode, touchNode, auth, f) - ) - ) - } else { - // image fields have a mime property among other - // maybe should find a better test - if (field !== null && field.hasOwnProperty('mime')) { - let fileNodeID - // using field on the cache key for multiple image field - const mediaDataCacheKey = `strapi-media-${item.id}-${key}` - const cacheMediaData = await cache.get(mediaDataCacheKey) + // image fields have a mime property among other + // maybe should find a better test + if(item && item.hasOwnProperty('mime')) { + let fileNodeID + // using field on the cache key for multiple image field + const mediaDataCacheKey = `strapi-media-${item.id}-${key}` + const cacheMediaData = await cache.get(mediaDataCacheKey) - // If we have cached media data and it wasn't modified, reuse - // previously created file node to not try to redownload - if (cacheMediaData && field.updatedAt === cacheMediaData.updatedAt) { - fileNodeID = cacheMediaData.fileNodeID - touchNode({ nodeId: cacheMediaData.fileNodeID }) - } + // If we have cached media data and it wasn't modified, reuse + // previously created file node to not try to redownload + if (cacheMediaData && item.updatedAt === cacheMediaData.updatedAt) { + fileNodeID = cacheMediaData.fileNodeID + touchNode({ nodeId: cacheMediaData.fileNodeID }) + } - // If we don't have cached data, download the file - if (!fileNodeID) { - try { - // full media url - const source_url = `${field.url.startsWith('http') ? '' : apiURL}${ - field.url - }` - const fileNode = await createRemoteFileNode({ - url: source_url, - store, - cache, - createNode, - auth, - }) + // If we don't have cached data, download the file + if (!fileNodeID) { + try { + // full media url + const source_url = `${item.url.startsWith('http') ? '' : apiURL}${ + item.url + }` + const fileNode = await createRemoteFileNode({ + url: source_url, + store, + cache, + createNode, + createNodeId, + auth, + }) - // If we don't have cached data, download the file - if (fileNode) { - fileNodeID = fileNode.id + // If we don't have cached data, download the file + if (fileNode) { + fileNodeID = fileNode.id - await cache.set(mediaDataCacheKey, { - fileNodeID, - modified: field.updatedAt, - }) - } - } catch (e) { - // Ignore - } - } - if (fileNodeID) { - item[`${key}___NODE`] = fileNodeID + await cache.set(mediaDataCacheKey, { + fileNodeID, + modified: item.updatedAt, + }) } + } catch (e) { + // Ignore + } + } + + if (fileNodeID) { + if(key === 'localFile') { + item[`${key}___NODE`] = fileNodeID + } + + return fileNodeID + } + } else if(Array.isArray(item)) { + await Promise.all( + item.map(async f => + extractFields( + apiURL, + store, + cache, + createNode, + createNodeId, + touchNode, + auth, + f + ) + ) + ) + } else if(item && typeof item === 'object') { + for(const key of Object.keys(item)) { + const field = item[key] + + const fileNodeID = await extractFields( + apiURL, + store, + cache, + createNode, + createNodeId, + touchNode, + auth, + field, + key + ) + + if (fileNodeID) { + item[`${key}___NODE`] = fileNodeID } } } @@ -77,6 +108,7 @@ exports.downloadMediaFiles = async ({ store, cache, createNode, + createNodeId, touchNode, jwtToken: auth, }) => @@ -89,6 +121,7 @@ exports.downloadMediaFiles = async ({ store, cache, createNode, + createNodeId, touchNode, auth, item @@ -96,4 +129,4 @@ exports.downloadMediaFiles = async ({ } return entity }) - ) + ) \ No newline at end of file From 0706cb9b7c8a9a1e6dd7cbecd56a1ddcf4342a23 Mon Sep 17 00:00:00 2001 From: denisgoryaynov Date: Sun, 17 Nov 2019 21:29:51 +0100 Subject: [PATCH 2/5] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9e09a35..aa74830 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gatsby-source-strapi", - "version": "0.0.10", + "version": "0.0.11", "description": "Gatsby source plugin for building websites using Strapi as a data source", "author": { "email": "hi@strapi.io", From d7d39aa93e3cc9bf313568b3da48350d33a27409 Mon Sep 17 00:00:00 2001 From: denisgoryaynov Date: Sun, 17 Nov 2019 21:36:13 +0100 Subject: [PATCH 3/5] Updated README --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 723349f..41d54ae 100644 --- a/README.md +++ b/README.md @@ -44,3 +44,25 @@ You can query Document nodes created from your Strapi API like the following: } } ``` + +To query images you can do the following: + +```graphql +{ + allStrapiArticle { + edges { + node { + id + singleImage { + publicURL + } + multipleImages { + localFile { + publicURL + } + } + } + } + } +} +``` From 143a71ef67ceaa8a6d4cea8c0966becb0752a247 Mon Sep 17 00:00:00 2001 From: denisgoryaynov Date: Mon, 18 Nov 2019 17:42:36 +0100 Subject: [PATCH 4/5] Update to normalize.js --- src/normalize.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/normalize.js b/src/normalize.js index 02ce7c2..be8c135 100644 --- a/src/normalize.js +++ b/src/normalize.js @@ -13,7 +13,7 @@ const extractFields = async ( ) => { // image fields have a mime property among other // maybe should find a better test - if(item && item.hasOwnProperty('mime')) { + if (item && item.hasOwnProperty('mime')) { let fileNodeID // using field on the cache key for multiple image field const mediaDataCacheKey = `strapi-media-${item.id}-${key}` @@ -32,7 +32,7 @@ const extractFields = async ( // full media url const source_url = `${item.url.startsWith('http') ? '' : apiURL}${ item.url - }` + }` const fileNode = await createRemoteFileNode({ url: source_url, store, @@ -57,13 +57,13 @@ const extractFields = async ( } if (fileNodeID) { - if(key === 'localFile') { - item[`${key}___NODE`] = fileNodeID + if (key !== 'localFile') { + return fileNodeID } - return fileNodeID + item.localFile___NODE = fileNodeID } - } else if(Array.isArray(item)) { + } else if (Array.isArray(item)) { await Promise.all( item.map(async f => extractFields( @@ -78,8 +78,8 @@ const extractFields = async ( ) ) ) - } else if(item && typeof item === 'object') { - for(const key of Object.keys(item)) { + } else if (item && typeof item === 'object') { + for (const key of Object.keys(item)) { const field = item[key] const fileNodeID = await extractFields( @@ -129,4 +129,4 @@ exports.downloadMediaFiles = async ({ } return entity }) - ) \ No newline at end of file + ) From d8c076531cbdc435b122aed51e9e58b58f602f1e Mon Sep 17 00:00:00 2001 From: denisgoryaynov Date: Mon, 18 Nov 2019 18:22:39 +0100 Subject: [PATCH 5/5] Fixed cache field in normalize.js --- src/normalize.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/normalize.js b/src/normalize.js index be8c135..d9c6f17 100644 --- a/src/normalize.js +++ b/src/normalize.js @@ -48,7 +48,7 @@ const extractFields = async ( await cache.set(mediaDataCacheKey, { fileNodeID, - modified: item.updatedAt, + updatedAt: item.updatedAt, }) } } catch (e) {